<?php

namespace App\Patient\Repository;

use App\Patient\Entity\PatientMedicalRecord;
use App\Patient\Entity\PatientRecord;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class PatientMedicalRecordRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, PatientMedicalRecord::class);
    }

    public function findByUuid(string $uuid): ?PatientMedicalRecord
    {
        return $this->findOneBy(['uuid' => $uuid]);
    }

    /** @return PatientMedicalRecord[] */
    public function findByRecord(PatientRecord $record): array
    {
        return $this->createQueryBuilder('m')
            ->where('m.record = :record')
            ->setParameter('record', $record)
            ->orderBy('m.recordedAt', 'DESC')
            ->getQuery()
            ->getResult();
    }

    public function save(PatientMedicalRecord $m): void
    {
        $this->getEntityManager()->persist($m);
        $this->getEntityManager()->flush();
    }

    public function remove(PatientMedicalRecord $m): void
    {
        $this->getEntityManager()->remove($m);
        $this->getEntityManager()->flush();
    }
}
