<?php

namespace App\Insurance\Repository;

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

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

    /** @return EntityInsurancePricing[] */
    public function findByEntity(string $entityType, int $entityId): array
    {
        return $this->findBy(['entityType' => $entityType, 'entityId' => $entityId]);
    }

    public function findOneForInsurance(string $entityType, int $entityId, ?int $insuranceId): ?EntityInsurancePricing
    {
        return $this->findOneBy([
            'entityType'  => $entityType,
            'entityId'    => $entityId,
            'insuranceId' => $insuranceId,
        ]);
    }

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

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