<?php

namespace App\Insurance\Service;

use App\Insurance\Entity\EntityInsurancePricing;
use App\Insurance\Entity\TenantInsurance;
use App\Insurance\Entity\TenantServiceCoverage;
use Doctrine\ORM\EntityManagerInterface;

/**
 * Removes the insurance configuration a doctor/clinic owns when that entity is
 * deleted. The owning columns (entity_type, entity_id) are polymorphic
 * (doctor|clinic), so no database FK can cascade this — the cleanup is enforced
 * here in the application. Coverage rows are removed via their owning
 * tenant_insurance ids.
 */
final class TenantInsuranceCleanupService
{
    public function __construct(private readonly EntityManagerInterface $em) {}

    public function purgeForEntity(string $entityType, int $entityId): void
    {
        $tenantInsuranceIds = $this->em->createQuery(
            'SELECT t.id FROM ' . TenantInsurance::class . ' t
              WHERE t.entityType = :type AND t.entityId = :id'
        )->setParameter('type', $entityType)
         ->setParameter('id', $entityId)
         ->getSingleColumnResult();

        if ($tenantInsuranceIds !== []) {
            $this->em->createQuery(
                'DELETE ' . TenantServiceCoverage::class . ' c
                  WHERE c.tenantInsuranceId IN (:ids)'
            )->setParameter('ids', $tenantInsuranceIds)->execute();
        }

        $this->em->createQuery(
            'DELETE ' . TenantInsurance::class . ' t
              WHERE t.entityType = :type AND t.entityId = :id'
        )->setParameter('type', $entityType)
         ->setParameter('id', $entityId)
         ->execute();

        $this->em->createQuery(
            'DELETE ' . EntityInsurancePricing::class . ' p
              WHERE p.entityType = :type AND p.entityId = :id'
        )->setParameter('type', $entityType)
         ->setParameter('id', $entityId)
         ->execute();
    }
}
