<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Marketing\Repository;

use CpCreation\VitiCore\Marketing\Model\AbandonedCartConfiguration;
use CpCreation\VitiCore\Marketing\Model\AbandonedCartRule;
use CpCreation\VitiCore\Repository\BaseRepository;
use Doctrine\Persistence\ManagerRegistry;

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

    /**
     * @return list<AbandonedCartRule>
     */
    public function findForConfiguration(AbandonedCartConfiguration $configuration): array
    {
        return $this->createQueryBuilder('rule')
            ->andWhere('rule.configuration = :configuration')
            ->setParameter('configuration', $configuration)
            ->orderBy('rule.position', 'ASC')
            ->addOrderBy('rule.delayHours', 'ASC')
            ->addOrderBy('rule.createdAt', 'ASC')
            ->getQuery()
            ->getResult();
    }

    /**
     * @return list<AbandonedCartRule>
     */
    public function findEnabledForConfiguration(AbandonedCartConfiguration $configuration): array
    {
        return $this->createQueryBuilder('rule')
            ->andWhere('rule.configuration = :configuration')
            ->andWhere('rule.enabled = TRUE')
            ->setParameter('configuration', $configuration)
            ->orderBy('rule.position', 'ASC')
            ->addOrderBy('rule.delayHours', 'ASC')
            ->getQuery()
            ->getResult();
    }
}
