<?php

namespace App\Staff\Repository;

use App\Staff\Entity\ClinicStaff;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

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

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

    public function findByEntity(string $entityType, int $entityId, bool $activeOnly = false): array
    {
        $qb = $this->createQueryBuilder('s')
            ->where('s.entityType = :type')
            ->andWhere('s.entityId = :id')
            ->setParameter('type', $entityType)
            ->setParameter('id', $entityId)
            ->orderBy('s.fullName', 'ASC');

        if ($activeOnly) {
            $qb->andWhere('s.active = true');
        }

        return $qb->getQuery()->getResult();
    }

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