<?php

namespace App\Repository;

use App\Entity\MemberCase;
use App\Entity\Referral;
use App\Enums\ReferralServiceType;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Persistence\ManagerRegistry;

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

    public function getActiveReferrals(MemberCase $case): array
    {
        $referrals = $this->createQueryBuilder('r')
            ->andWhere('r.dischargeDate IS NULL')
            ->andWhere('r.memberCase = :case')
            ->setParameter('case', $case->getId()->toBinary())
            ->orderBy('r.hours', 'ASC')
            ->getQuery()
            ->getResult()
        ;

        foreach ($referrals as $idx => $ref) {
            $this->populateNotes($referrals[$idx]);
        }

        return $referrals;
    }

    public function getClosedReferrals(MemberCase $case): array
    {
        $referrals = $this->createQueryBuilder('r')
            ->andWhere('r.dischargeDate IS NOT NULL')
            ->andWhere('r.memberCase = :case')
            ->setParameter('case', $case->getId()->toBinary())
            ->orderBy('r.dischargeDate', 'DESC')
            ->getQuery()
            ->getResult()
        ;

        foreach ($referrals as $idx => $ref) {
            $this->populateNotes($referrals[$idx]);
        }

        return $referrals;
    }

    public function populateNotes(Referral &$referral): void
    {
        $noteType = 'App\Entity\StandardNote';

        if ($referral->getServiceCode() == ReferralServiceType::VS_THBB) {
            $noteType = 'App\Entity\VisitNote';
        }

        $query = $this->getEntityManager()->createQuery("
            SELECT n
            FROM $noteType n
            WHERE n.referral = :referral
        ");
        $query->setParameter('referral', $referral->getId()->toBinary());
        $res = $query->getResult();

        if (!count($res)){
            return;
        }

        $referral->setNotes(new ArrayCollection($res));
    }

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

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