<?php

namespace App\Resource\Repository;

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

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

    /** @return NationalHoliday[] */
    public function findForYear(int $jalaliYear): array
    {
        return $this->createQueryBuilder('h')
            ->where('h.jalaliYear = :year')
            ->setParameter('year', $jalaliYear)
            ->orderBy('h.date', 'ASC')
            ->getQuery()
            ->getResult();
    }

    /**
     * @return array<int, NationalHoliday> کلید = نیمه‌شب همان روز
     */
    public function mapForRange(int $from, int $to): array
    {
        $rows = $this->createQueryBuilder('h')
            ->where('h.date >= :from')
            ->andWhere('h.date <= :to')
            ->setParameter('from', $from)
            ->setParameter('to', $to)
            ->getQuery()
            ->getResult();

        $map = [];
        foreach ($rows as $holiday) {
            $map[$holiday->getDate()] = $holiday;
        }

        return $map;
    }

    public function findByDate(int $date): ?NationalHoliday
    {
        return $this->findOneBy(['date' => $date]);
    }
}
