<?php

declare(strict_types=1);

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

use CpCreation\VitiCore\Cart\Model\PaymentMethod;
use CpCreation\VitiCore\Repository\BaseRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;

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

    public function findAvailableForAdminOrderQuery(): QueryBuilder
    {
        return $this->createQueryBuilder('p')
            ->andWhere('p.disabled = false')
            ->andWhere('p.deletedAt IS NULL')
            ->addOrderBy('p.position', 'ASC');
    }

    public function findAvailableForAdminOrderById(string $id): ?PaymentMethod
    {
        return $this->findAvailableForAdminOrderQuery()
            ->andWhere('p.id = :id')
            ->setParameter('id', $id)
            ->getQuery()
            ->getOneOrNullResult();
    }

    public function findByDefaultForPaymentOnline(): ?PaymentMethod
    {
        return $this->createQueryBuilder('p')
            ->andWhere('p.paymentOnline = true')
            ->setMaxResults(1)
            ->getQuery()
            ->getOneOrNullResult();
    }
}
