<?php

namespace App\Repository;

use App\Entity\CaseItinerary;
use App\Entity\MemberCase;
use App\Entity\User;
use App\Entity\UserCase;
use DateTime;
use DateTimeZone;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

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

    public function getRecentTravel(User $user, ?array $params = null): ?array
    {
        $query = $this->createQueryBuilder('ci')
            ->leftJoin(UserCase::class, 'uc', 'WITH', 'uc.memberCase = ci.memberCase')
            ->andWhere('uc.user = :user')
            ->setParameter('user', $user->getId()->toBinary())
        ;

        if (isset($params['case']) && is_a($params['case'], MemberCase::class)) {
            $query->andWhere('ci.memberCase = :case')->setParameter('case', $params['case']->getId()->toBinary());
        }

        if (isset($params['from']) && is_a($params['from'], \DateTime::class)) {
            $query->andWhere('ci.date >= :from')->setParameter('from', $params['from']->format('Y-m-d'));
        }

        if (isset($params['to']) && is_a($params['to'], \DateTime::class)) {
            $query->andWhere('ci.date <= :to')->setParameter('to', $params['to']->format('Y-m-d'));
        }

        $query->orderBy('ci.date', 'DESC')
            ->addOrderBy('ci.arrival', 'DESC');

        //dd($query->getQuery());

        return $query->getQuery()->getResult();
    }

    public function getYTDTravel(User $user): ?array
    {
        $startDate = new DateTime('now', new DateTimeZone($_ENV['COMPANY_TIMEZONE']));

        $query = $this->createQueryBuilder('ci')
            ->leftJoin(UserCase::class, 'uc', 'WITH', 'uc.memberCase = ci.memberCase')
            ->andWhere('uc.user = :user')
            ->setParameter('user', $user->getId()->toBinary())
            ->andWhere('ci.date >= :from')
            ->setParameter('from', $startDate->format('Y-01-01'))
            ->orderBy('ci.date', 'DESC')
            ->addOrderBy('ci.arrival', 'DESC')
        ;

        return $query->getQuery()->getResult();
    }

    public function getTravelLast30Days(User $user): ?array
    {
        $startDate = new DateTime('now', new DateTimeZone($_ENV['COMPANY_TIMEZONE']));

        $query = $this->createQueryBuilder('ci')
            ->leftJoin(UserCase::class, 'uc', 'WITH', 'uc.memberCase = ci.memberCase')
            ->andWhere('uc.user = :user')
            ->setParameter('user', $user->getId()->toBinary())
            ->andWhere('ci.date >= :from')
            ->setParameter('from', $startDate->modify('-30 days')->format('Y-m-d'))
            ->orderBy('ci.date', 'DESC')
            ->addOrderBy('ci.arrival', 'DESC')
        ;

        return $query->getQuery()->getResult();
    }

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

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