<?php

namespace App\Insurance\Repository;

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

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

    /** @return Insurance[] */
    public function findActive(?InsuranceType $type = null): array
    {
        $qb = $this->createQueryBuilder('i')
            ->where('i.status = 1')
            ->orderBy('i.name', 'ASC');

        if ($type !== null) {
            $qb->andWhere('i.type = :type')->setParameter('type', $type->value);
        }

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

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

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