<?php

namespace App\Repository;

use App\Entity\Referral;
use App\Entity\StandardNote;
use App\Entity\User;
use App\Entity\UserCase;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

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

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

    public function filterNotes(User $user, array $params = []): array
    {
        $query = $this->createQueryBuilder('s')
            ->leftJoin(Referral::class, 'r', 'WITH', 's.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('s.referral = :referral')
                ->setParameter('referral', $params['referral']->getId()->toBinary());
        }

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

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

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

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

    //    /**
    //     * @return StandardNote[] Returns an array of StandardNote 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): ?StandardNote
    //    {
    //        return $this->createQueryBuilder('s')
    //            ->andWhere('s.exampleField = :val')
    //            ->setParameter('val', $value)
    //            ->getQuery()
    //            ->getOneOrNullResult()
    //        ;
    //    }
}
