<?php

namespace App\Tests\Insurance;

use App\Doctor\Entity\Doctor;
use App\Insurance\Entity\EntityInsurancePricing;
use App\Insurance\Entity\TenantInsurance;
use App\Insurance\Entity\TenantServiceCoverage;
use App\Insurance\Service\TenantInsuranceCleanupService;
use App\Tests\ApiTestCase;

/**
 * Deleting a doctor/clinic must purge the insurance config it owns. entity_id is
 * polymorphic, so this cleanup is enforced in the application, not by a DB FK.
 */
class TenantInsuranceCleanupTest extends ApiTestCase
{
    public function testPurgeRemovesTenantInsurancePricingAndCoverage(): void
    {
        $entityType = TenantInsurance::TYPE_DOCTOR;
        $entityId   = random_int(1_000_000, 9_999_999); // a doctor id not shared with other tests

        $tenant = new TenantInsurance($entityType, $entityId, 1);
        $this->em->persist($tenant);
        $this->em->flush();

        $coverage = new TenantServiceCoverage($tenant->getId(), 1);
        $this->em->persist($coverage);
        $this->em->persist(new EntityInsurancePricing($entityType, $entityId, 1));
        $this->em->flush();

        static::getContainer()->get(TenantInsuranceCleanupService::class)
            ->purgeForEntity($entityType, $entityId);

        $this->em->clear();

        $this->assertCount(0, $this->em->getRepository(TenantInsurance::class)
            ->findBy(['entityType' => $entityType, 'entityId' => $entityId]));
        $this->assertCount(0, $this->em->getRepository(EntityInsurancePricing::class)
            ->findBy(['entityType' => $entityType, 'entityId' => $entityId]));
        $this->assertNull($this->em->getRepository(TenantServiceCoverage::class)
            ->findOneBy(['tenantInsuranceId' => $tenant->getId()]));
    }

    /**
     * End-to-end: the DELETE doctor endpoint must trigger the purge. Without the
     * wiring the doctor is removed but its insurance rows are left orphaned.
     */
    public function testDeleteDoctorEndpointPurgesInsuranceConfig(): void
    {
        $doctor = new Doctor($this->createUser(['ROLE_DOCTOR']), 'دکتر حذف');
        $this->em->persist($doctor);
        $this->em->flush();
        $doctorId = $doctor->getId();

        $tenant = new TenantInsurance(TenantInsurance::TYPE_DOCTOR, $doctorId, 1);
        $this->em->persist($tenant);
        $this->em->persist(new EntityInsurancePricing(TenantInsurance::TYPE_DOCTOR, $doctorId, 1));
        $this->em->flush();

        $admin = $this->createUser(['ROLE_ADMIN']);
        $this->authJson('DELETE', '/api/v1/doctor/' . $doctor->getUuid(), $admin);
        $this->assertSame(200, $this->responseCode());

        $this->em->clear();
        $this->assertCount(0, $this->em->getRepository(TenantInsurance::class)
            ->findBy(['entityType' => TenantInsurance::TYPE_DOCTOR, 'entityId' => $doctorId]));
        $this->assertCount(0, $this->em->getRepository(EntityInsurancePricing::class)
            ->findBy(['entityType' => TenantInsurance::TYPE_DOCTOR, 'entityId' => $doctorId]));
    }
}
