<?php

namespace App\Appointment\Booking\Repository;

use App\Appointment\Booking\Entity\AppointmentHold;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

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

    public function findByUuid(string $uuid): ?AppointmentHold
    {
        return $this->findOneBy(['uuid' => $uuid]);
    }

    /**
     * hold هایی که مهلتشان گذشته و هنوز تبدیل به نوبت نشده‌اند.
     *
     * @return AppointmentHold[]
     */
    public function findExpired(int $now, int $limit = 200): array
    {
        return $this->createQueryBuilder('h')
            ->where('h.expiresAt <= :now')
            ->andWhere('h.confirmedAt IS NULL')
            ->setParameter('now', $now)
            ->setMaxResults($limit)
            ->getQuery()
            ->getResult();
    }
}
