<?php

/*
 * This file is part of the VitiCore 2.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Depot\Repository;

use CpCreation\VitiCore\Depot\Model\Depot;
use CpCreation\VitiCore\Repository\BaseRepository;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\Persistence\ManagerRegistry;

class DepotRepository extends BaseRepository
{
    /**
     * @throws \ReflectionException
     */
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Depot::class);
    }

    /**
     * @return array<int, Depot>
     */
    public function findByCategory(string $slug): array
    {
        return $this
            ->createQueryBuilder('depot')
            ->innerJoin('depot.translations', 'tr', Join::WITH, 'tr.translatable = depot.id')
            ->innerJoin('depot.category', 'ca', Join::WITH, 'ca.id = depot.category')
            ->innerJoin('ca.translations', 'catr', Join::WITH, 'catr.translatable = ca.id')
            ->andWhere('catr.slug = :slug')
            ->andWhere('depot.disabled = FALSE')
            ->orderBy('depot.postal', 'ASC')
            ->setParameter('slug', $slug)
            ->getQuery()
            ->getResult();
    }

    /**
     * @return array<int, Depot>
     */
    public function findByCategoryOrderName(string $slug): array
    {
        return $this
            ->createQueryBuilder('depot')
            ->innerJoin('depot.translations', 'tr', Join::WITH, 'tr.translatable = depot.id')
            ->innerJoin('depot.category', 'ca', Join::WITH, 'ca.id = depot.category')
            ->innerJoin('ca.translations', 'catr', Join::WITH, 'catr.translatable = ca.id')
            ->andWhere('catr.slug = :slug')
            ->andWhere('depot.disabled = FALSE')
            ->orderBy('tr.name', 'ASC')
            ->setParameter('slug', $slug)
            ->getQuery()
            ->getResult();
    }

    /**
     * @return array<int, Depot>
     */
    public function findAllOrderByCountryOrderByCategoryOrderPostalAll(): array
    {
        return $this
            ->createQueryBuilder('depot')
            ->leftJoin('depot.translations', 'tr')
            ->leftJoin('depot.category', 'ca')
            ->leftJoin('ca.translations', 'catr')
            ->addOrderBy('ca.position', 'ASC')
            ->addOrderBy('depot.country', 'ASC')
            ->addOrderBy('depot.postal', 'ASC')
            ->getQuery()
            ->getResult();
    }

    /**
     * @return array<int, Depot>
     */
    public function findAllOrderByCountryOrderByCategoryOrderPosition(): array
    {
        return $this
            ->createQueryBuilder('depot')
            ->leftJoin('depot.translations', 'tr')
            ->leftJoin('depot.category', 'ca')
            ->leftJoin('ca.translations', 'catr')
            ->addOrderBy('ca.position', 'ASC')
            ->addOrderBy('depot.position', 'ASC')
            ->andWhere('depot.disabled = FALSE')
            ->getQuery()
            ->getResult();
    }

    /**
     * @return array<int, Depot>
     */
    public function findAllOrderByCountryOrderByCategoryOrderPositionAdmin(): array
    {
        return $this
            ->createQueryBuilder('depot')
            ->leftJoin('depot.translations', 'tr')
            ->leftJoin('depot.category', 'ca')
            ->leftJoin('ca.translations', 'catr')
            ->addOrderBy('ca.position', 'ASC')
            ->addOrderBy('depot.position', 'ASC')
            ->getQuery()
            ->getResult();
    }
}
