<?php

namespace App\Insurance\Repository;

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

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

    public function findOneFor(int $tenantInsuranceId, int $serviceItemId): ?TenantServiceCoverage
    {
        return $this->findOneBy([
            'tenantInsuranceId' => $tenantInsuranceId,
            'serviceItemId'     => $serviceItemId,
        ]);
    }

    /** @return TenantServiceCoverage[] */
    public function findByContract(int $tenantInsuranceId): array
    {
        return $this->findBy(['tenantInsuranceId' => $tenantInsuranceId]);
    }

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

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