<?php

namespace App\Repository;

use App\Entity\Service;
use App\Entity\ServiceRdvReservation;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @extends ServiceEntityRepository<Service>
 *
 * @method Service|null find($id, $lockMode = null, $lockVersion = null)
 * @method Service|null findOneBy(array $criteria, array $orderBy = null)
 * @method Service[]    findAll()
 * @method Service[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class ServiceRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Service::class);
    }

    /**
     * @return Service[] Returns an array of Service objects
     */
    public function show(): array
    {
        $serviceDate = $this->showLimitationByDate();
        $serviceRDV = $this->showLimitationByRDV();
        return array_merge($serviceDate, $serviceRDV);
    }

    /**
     * @return Service[] Returns an array of Service objects
     */
    public function showLimitationByDate(): array
    {
        return $this->createQueryBuilder('s')
            ->Where('s.active = :Active')
            ->andWhere('s.limit_type = 0')
            ->andWhere('s.limit_date >= :LimitDate')
            ->setParameter('Active', true)
            ->setParameter('LimitDate', new \DateTime())
            ->getQuery()
            ->getResult()
        ;
    }


    /**
     * @return Service[] Returns an array of Service objects
     */
    public function showLimitationByRDV(): array
    {
        $data =  $this->createQueryBuilder('s')
                    ->select('S.id')
                    ->from(ServiceRdvReservation::class, 'R')
                    ->innerJoin('R.service', 'S')
                    ->Where('R.disponible = :Dispo')->setParameter('Dispo', true)
                    ->andWhere('R.dateDebut > :DateDebut')->setParameter('DateDebut', new \DateTime())
                    ->groupBy('S.id')
                    ->getQuery()
                    ->getResult()
                ;
        $ListId = array_column($data, 'id');

        return $this->findBy(['id'=>$ListId, 'active'=>true]);
    }


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

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