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

use CpCreation\VitiCore\Marketing\Model\ReviewRequestConfiguration;
use CpCreation\VitiCore\Marketing\ProductReviewConfigurationProvider;
use CpCreation\VitiCore\Repository\BaseRepository;
use Doctrine\Persistence\ManagerRegistry;

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

    public function findOne(): ?ReviewRequestConfiguration
    {
        return $this->createQueryBuilder('configuration')
            ->orderBy('configuration.createdAt', 'DESC')
            ->setMaxResults(1)
            ->getQuery()
            ->getOneOrNullResult();
    }

    public function isEnabled(): bool
    {
        return $this->findOne()?->isEnabled() ?? false;
    }

    public function isModerationRequired(): bool
    {
        return $this->findOne()?->isModerationRequired() ?? true;
    }
}
