<?php

declare(strict_types=1);

namespace App\Repository;

use App\Entity\Event;
use App\Entity\UserTrip;
use App\Entity\UserTripEvent;
use DateTimeImmutable;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

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

    public function save(UserTripEvent $userTripEvent, bool $flush = true): void
    {
        $this->getEntityManager()->persist($userTripEvent);

        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }

    public function remove(UserTripEvent $userTripEvent, bool $flush = true): void
    {
        $this->getEntityManager()->remove($userTripEvent);

        if ($flush) {
            $this->getEntityManager()->flush();
        }
    }

    /**
     * @return UserTripEvent[]
     */
    public function findByUserTrip(UserTrip $userTrip): array
    {
        return $this->createQueryBuilder('ute')
            ->andWhere('ute.userTrip = :userTrip')
            ->setParameter('userTrip', $userTrip)
            ->orderBy('ute.date', 'ASC')
            ->getQuery()
            ->getResult();
    }

    /**
     * @return UserTripEvent[]
     */
    public function findByEvent(Event $event): array
    {
        return $this->createQueryBuilder('ute')
            ->andWhere('ute.event = :event')
            ->setParameter('event', $event)
            ->orderBy('ute.date', 'ASC')
            ->getQuery()
            ->getResult();
    }

    /**
     * @return UserTripEvent[]
     */
    public function findByDateRange(DateTimeImmutable $start, DateTimeImmutable $end): array
    {
        return $this->createQueryBuilder('ute')
            ->andWhere('ute.date >= :start')
            ->andWhere('ute.date <= :end')
            ->setParameter('start', $start)
            ->setParameter('end', $end)
            ->orderBy('ute.date', 'ASC')
            ->getQuery()
            ->getResult();
    }

    /**
     * @return UserTripEvent[]
     */
    public function findByUserTripAndDateRange(UserTrip $userTrip, DateTimeImmutable $start, DateTimeImmutable $end): array
    {
        return $this->createQueryBuilder('ute')
            ->andWhere('ute.userTrip = :userTrip')
            ->andWhere('ute.date >= :start')
            ->andWhere('ute.date <= :end')
            ->setParameter('userTrip', $userTrip)
            ->setParameter('start', $start)
            ->setParameter('end', $end)
            ->orderBy('ute.date', 'ASC')
            ->getQuery()
            ->getResult();
    }

    /**
     * @return UserTripEvent|null
     */
    public function findOneByUserTripAndEvent(UserTrip $userTrip, Event $event): ?UserTripEvent
    {
        return $this->createQueryBuilder('ute')
            ->andWhere('ute.userTrip = :userTrip')
            ->andWhere('ute.event = :event')
            ->setParameter('userTrip', $userTrip)
            ->setParameter('event', $event)
            ->getQuery()
            ->getOneOrNullResult();
    }

    /**
     * @return UserTripEvent[]
     */
    public function findRecentEvents(int $limit = 10): array
    {
        return $this->createQueryBuilder('ute')
            ->orderBy('ute.createdAt', 'DESC')
            ->setMaxResults($limit)
            ->getQuery()
            ->getResult();
    }
}