<?php
declare(strict_types=1);

namespace App\Repository\Booking;

use App\Entity\Booking\Course;
use App\Entity\Organization\Organization;
use DateTimeInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class CourseRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Course::class);
    }

    /**
     * Récupère les cours d'une organisation d'une date à une autre
     * @param Organization $organization
     * @param DateTimeInterface $start
     * @param DateTimeInterface $end
     * @return list<Course>
     */
    public function getCoursesToFrom(Organization $organization, DateTimeInterface $start, DateTimeInterface $end): array
    {
        $this->_em->getFilters()->disable('activity_year_filter');

        $queryBuilder = $this->createQueryBuilder('course');

        $queryBuilder
            ->andWhere(
                $queryBuilder->expr()->between('course.datetimeStart', ':dateStart', ':dateEnd')
            )
            ->andWhere('course.organization = :organization')
            ->setParameter('dateStart', $start)
            ->setParameter('dateEnd', $end)
            ->setParameter('organization', $organization)
        ;

        $results = $queryBuilder->getQuery()->getResult();

        $this->_em->getFilters()->enable('activity_year_filter');

        return $results;
    }
}
