<?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\Product\Repository;

use CpCreation\VitiCore\Product\Model\ProductPriceCode;
use CpCreation\VitiCore\Repository\BaseRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\Persistence\ManagerRegistry;

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

    /**
     * @return ProductPriceCode[]
     */
    public function findByGroupOrderAdmin(): array
    {
        return $this->createQueryBuilder('r')
            ->andWhere('r.deletedAt IS NULL')
            ->orderBy('r.group', 'ASC')
            ->getQuery()
            ->getResult();
    }

    /**
     * @throws NonUniqueResultException
     */
    public function findOneCode(string $code): ?ProductPriceCode
    {
        return $this->createQueryBuilder('r')
            ->andWhere('r.code = :code')
            ->andWhere('r.deletedAt IS NULL')
            ->andWhere('r.disabled = false')
            ->setParameter('code', $code)
            ->getQuery()
            ->getOneOrNullResult();
    }

    /**
     * @throws NonUniqueResultException
     */
    public function findOneViewHt(string $code): ?ProductPriceCode
    {
        return $this->createQueryBuilder('r')
            // À adapter selon le vrai nom du champ :
            // ->andWhere('r.viewHt = true')
            ->andWhere('r.code = :code')
            ->andWhere('r.deletedAt IS NULL')
            ->setParameter('code', $code)
            ->getQuery()
            ->getOneOrNullResult();
    }
}
