<?php

namespace App\ClinicService\Repository;

use App\ClinicService\Entity\ServiceItem;
use App\ClinicService\Entity\ServiceSection;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

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

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

    public function findBySection(ServiceSection $section): array
    {
        return $this->createQueryBuilder('i')
            ->where('i.section = :section')
            ->setParameter('section', $section)
            ->orderBy('i.name', 'ASC')
            ->getQuery()
            ->getResult();
    }

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

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