<?php

namespace App\Repository;

use App\Entity\Referral;
use App\Entity\UserCase;
use App\Entity\User;
use App\Entity\VisitNote;
use App\Entity\VisitNoteMembers;
use DateTimeImmutable;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

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

    public function getOrderedNotes(Referral $referral): array
    {
        return $this->createQueryBuilder('v')
            ->andWhere('v.referral = :referral')
            ->setParameter('referral', $referral->getId()->toBinary())
            ->orderBy('v.date', 'DESC')
            ->addOrderBy('v.startTime', 'DESC')
            ->getQuery()
            ->getResult()
        ;
    }

    public function filterNotes(User $user, array $params = []): array
    {
        $query = $this->createQueryBuilder('v')
            ->leftJoin(Referral::class, 'r', 'WITH', 'v.referral = r.id')
            ->leftJoin(UserCase::class, 'uc', 'WITH', 'r.memberCase = uc.memberCase')
            ->andWhere('uc.user = :user')
            ->setParameter('user', $user->getId()->toBinary())
        ;

        if ($params['case']) {
            $query->andWhere('r.memberCase = :memberCase')
                ->setParameter('memberCase', $params['case']->getId()->toBinary());
        }

        if ($params['referral']) {
            $query->andWhere('v.referral = :referral')
                ->setParameter('referral', $params['referral']->getId()->toBinary());
        }

        if ($params['startDate']) {
            $query->andWhere('v.date >= :startDate')
                ->setParameter('startDate', $params['startDate']);
        }

        if ($params['endDate']) {
            $query->andWhere('v.date <= :endDate')
                ->setParameter('endDate', $params['endDate']);
        }

        return $query->orderBy('v.date', 'DESC')
            ->addOrderBy('v.startTime', 'DESC')
            ->getQuery()
            ->getResult()
        ;
    }

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

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