<?php

namespace App\Patient\Repository;

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

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

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

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

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

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