<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (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\Product\Repository;

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

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

    /**
     * @return ProductCategory[]
     */
    public function findOnlyShop(?string $groupPrice = null): array
    {
        $qb = $this->createQueryBuilder('cat')
            ->select('DISTINCT cat, tr')
            ->addSelect('tr')
            ->innerJoin('cat.translations', 'tr', Join::WITH, 'tr.translatable = cat.id')
            ->andWhere('cat.pageShop = TRUE')
            ->orderBy('cat.position', 'ASC');

        if (null === $groupPrice) {
            $qb->innerJoin(
                'cat.products',
                'pr',
                Join::WITH,
                'pr.category = cat.id AND pr.shop = TRUE AND pr.disabled = FALSE AND pr.deletedAt IS NULL'
            );
        } else {
            $qb
                ->leftJoin(
                    'cat.products',
                    'groupProduct',
                    Join::WITH,
                    'groupProduct.category = cat.id AND groupProduct.shop = TRUE AND groupProduct.disabled = FALSE AND groupProduct.deletedAt IS NULL'
                )
                ->leftJoin('groupProduct.groups', 'gr', Join::WITH, 'gr.id = :group')
                ->andWhere('(gr.id IS NOT NULL OR cat.displayEvenWithoutGroupMatch = TRUE)')
                ->setParameter('group', $groupPrice);
        }

        return $qb->getQuery()->getResult();
    }

    public function findOneBySlug(string $slug): ?ProductCategory
    {
        return $this->createQueryBuilder('cat')
            ->addSelect('translation')
            ->innerJoin('cat.translations', 'matchingTranslation', Join::WITH, 'matchingTranslation.translatable = cat.id')
            ->leftJoin('cat.translations', 'translation', Join::WITH, 'translation.translatable = cat.id')
            ->andWhere('matchingTranslation.slug = :slug')
            ->setParameter('slug', $slug)
            ->getQuery()
            ->getOneOrNullResult();
    }

    public function findFirstCuvee(): ?ProductCategory
    {
        return $this->createQueryBuilder('cat')
            ->addSelect('tr')
            ->innerJoin('cat.translations', 'tr', Join::WITH, 'tr.translatable = cat.id')
            ->innerJoin(
                'cat.products',
                'pr',
                Join::WITH,
                'pr.category = cat.id AND pr.cuvee = TRUE AND pr.disabled = FALSE AND pr.deletedAt IS NULL'
            )
            ->andWhere('cat.pageCuvee = TRUE')
            ->orderBy('cat.position', 'ASC')
            ->setMaxResults(1)
            ->getQuery()
            ->getOneOrNullResult();
    }

    public function hasCommerceExportEnabled(): bool
    {
        return 0 < (int) $this->createQueryBuilder('category')
            ->select('COUNT(category.id)')
            ->andWhere('category.commerceExportEnabled = TRUE')
            ->getQuery()
            ->getSingleScalarResult();
    }
}
