<?php

declare(strict_types=1);

/*
 * 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\TransactionalEmail\Repository;

use CpCreation\VitiCore\Repository\BaseRepository;
use CpCreation\VitiCore\TransactionalEmail\Model\TransactionalEmail;
use Doctrine\Persistence\ManagerRegistry;

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

    /**
     * @return list<TransactionalEmail>
     */
    public function findEnabledByTrigger(string $triggerEvent): array
    {
        return $this->createQueryBuilder('transactionalEmail')
            ->andWhere('transactionalEmail.triggerEvent = :triggerEvent')
            ->andWhere('transactionalEmail.disabled = FALSE')
            ->setParameter('triggerEvent', $triggerEvent)
            ->orderBy('transactionalEmail.delayDays', 'ASC')
            ->addOrderBy('transactionalEmail.name', 'ASC')
            ->getQuery()
            ->getResult();
    }
}
