<?php

namespace App\Repository;

use App\Entity\ProductReserve;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @extends ServiceEntityRepository<ProductReserve>
 */
class ProductReserveRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, ProductReserve::class);
    }

    public function checkAvailability(ProductReserve $productReserve): bool
    {
        $qb = $this->createQueryBuilder('p');
        $qb->andWhere('p.product = :product')
            ->andWhere('p.startAt < :endAt')
            ->andWhere('p.endAt > :startAt')
            ->setParameter('product', $productReserve->getProduct())
            ->setParameter('startAt', $productReserve->getStartAt())
            ->setParameter('endAt', $productReserve->getEndAt());

        return count($qb->getQuery()->getResult()) === 0;
    }

    //    /**
    //     * @return ProductReserve[] Returns an array of ProductReserve objects
    //     */
    //    public function findByExampleField($value): array
    //    {
    //        return $this->createQueryBuilder('p')
    //            ->andWhere('p.exampleField = :val')
    //            ->setParameter('val', $value)
    //            ->orderBy('p.id', 'ASC')
    //            ->setMaxResults(10)
    //            ->getQuery()
    //            ->getResult()
    //        ;
    //    }

    //    public function findOneBySomeField($value): ?ProductReserve
    //    {
    //        return $this->createQueryBuilder('p')
    //            ->andWhere('p.exampleField = :val')
    //            ->setParameter('val', $value)
    //            ->getQuery()
    //            ->getOneOrNullResult()
    //        ;
    //    }
}
