<?php

namespace App\Repository;

use App\Entity\Event;
use App\Entity\Gamemaster;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\DBAL\Types\Types;

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

    /*
     * Extraire les événements :
     * - public = true
     * - published = true
     * - endOn > now
     */
    public function findEventsToCome(bool $public=true): array
    {
        // Et il faut la date du moment
        $dateNow = new \DateTime('now');

        $qb = $this->createQueryBuilder('e')
            ->where('e.published = :published')
            ->andWhere('e.endOn > :dateNow')
            ->setParameter('published', true)
            ->setParameter('dateNow', $dateNow);

        // Prise en compte public / privé
        if ($public) {
            $qb->andWhere('(e.private = false OR e.private IS NULL)');
            }
        
        $query = $qb->orderBy('e.startOn', 'ASC')
           ->getQuery();

        return $query->getResult();
    }
    
    /*
     * Extraire les événements d'une année / mois
     * - published = true
     * - startOn >= 01/mois/année
     * - startOn < 01/mois+1/année
     */
    public function findEventsOfMonth(int $month, int $year, bool $public=true): array
    {
        // On génère les dates
        $dateStartMonth = new \DateTime(sprintf('%04d-%02d-01 00:00:00', $year, $month));
        $dateEndMonth = clone $dateStartMonth;
        $dateEndMonth->modify('last day of this month')->setTime(23, 59, 59);

        // Construction de la requête
        $qb = $this->createQueryBuilder('e')
        ->where('e.published = :published')
        ->andWhere('e.startOn >= :dateStartMonth')
        ->andWhere('e.endOn <= :dateEndMonth')
        ->setParameter('published', true)
        ->setParameter('dateStartMonth', $dateStartMonth)
        ->setParameter('dateEndMonth', $dateEndMonth);
        
        // Prise en compte public / privé
        if ($public) {
            $qb->andWhere('(e.private = false OR e.private IS NULL)');
        }
        
        $query = $qb->orderBy('e.startOn', 'ASC')
        ->getQuery();
        
        return $query->getResult();
    }

    /**
     * @return Event[]
     */
    public function findEventsToPrepare(?Gamemaster $gamemaster): array
    {
        // Et il faut la date du moment
        $dateNow = new \DateTime('now');

        // 19-09-2025 : suppression du statut "non publié" pour un EVT
        $qb = $this->createQueryBuilder('e')
             ->where('e.endOn > :dateNow')
             ->setParameter('dateNow', $dateNow)
              ;

        // Ne marche pas, va savoir ! => ajouter ?Gamemaster $gamemaster dans la function
        // TODO: à revoir
        // if ($gamemaster) {
        //     $qb->andWhere(':gamemaster MEMBER OF e.gamemastersAssigned')
        //     ->setParameter('gamemaster', $gamemaster);
        //}
        
        $query = $qb->orderBy('e.startOn', 'ASC')
           ->getQuery();

        $result = $query->getResult();
        
        if ($gamemaster) {
            $result = array_filter($result, function (Event $event) use ($gamemaster) {
                return $event->getGamemastersAssigned()->contains($gamemaster);
            });
        }

        return array_values($result);
}


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

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