<?php

namespace App\DoctorService\Repository;

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

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

    /** @return DoctorService[] */
    public function findActive(?int $specialtyId = null): array
    {
        $qb = $this->createQueryBuilder('ds')
            ->where('ds.status = 1')
            ->orderBy('ds.weight', 'ASC')
            ->addOrderBy('ds.name', 'ASC');

        if ($specialtyId !== null) {
            $qb->andWhere('ds.specialty = :specialty')->setParameter('specialty', $specialtyId);
        }

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

    public function save(DoctorService $service, bool $flush = true): void
    {
        $this->getEntityManager()->persist($service);
        if ($flush) $this->getEntityManager()->flush();
    }

    public function remove(DoctorService $service, bool $flush = true): void
    {
        $this->getEntityManager()->remove($service);
        if ($flush) $this->getEntityManager()->flush();
    }
}
