<?php

namespace App\Insurance\Controller;

use App\Auth\Entity\User;
use App\ClinicService\Repository\ServiceItemRepository;
use App\Clinic\Repository\ClinicRepository;
use App\Doctor\Repository\DoctorRepository;
use App\Insurance\Entity\DoctorInsurance;
use App\Insurance\Entity\EntityInsurancePricing;
use App\Insurance\Entity\Insurance;
use App\Insurance\Enum\InsuranceType;
use App\Insurance\Entity\TenantInsurance;
use App\Insurance\Repository\DoctorInsuranceRepository;
use App\Insurance\Repository\EntityInsurancePricingRepository;
use App\Insurance\Repository\InsuranceRepository;
use App\Insurance\Repository\TenantInsuranceRepository;
use App\Insurance\Repository\TenantServiceCoverageRepository;
use App\Insurance\Service\TenantInsuranceService;
use App\Shared\Constant\ErrorCodes;
use App\Shared\Controller\BaseController;
use App\Shared\Service\FileValidatorService;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Uid\Uuid;
use OpenApi\Attributes as OA;

#[OA\Tag(name: 'Insurance')]
class InsuranceController extends BaseController
{
    public function __construct(
        private readonly InsuranceRepository       $insuranceRepo,
        private readonly DoctorInsuranceRepository $doctorInsuranceRepo,
        private readonly DoctorRepository          $doctorRepo,
        private readonly ClinicRepository          $clinicRepo,
        private readonly EntityInsurancePricingRepository $pricingRepo,
        private readonly TenantInsuranceRepository $tenantInsuranceRepo,
        private readonly TenantServiceCoverageRepository $serviceCoverageRepo,
        private readonly TenantInsuranceService    $tenantInsuranceService,
        private readonly ServiceItemRepository     $serviceItemRepo,
        private readonly FileValidatorService      $fileValidator,
        private readonly string                    $projectDir,
    ) {}

    private function resolveEntity(User $user): array
    {
        if ($user->hasRole('ROLE_DOCTOR')) {
            $doctor = $this->doctorRepo->findByUser($user);
            return $doctor !== null ? [EntityInsurancePricing::TYPE_DOCTOR, $doctor->getId()] : [EntityInsurancePricing::TYPE_DOCTOR, null];
        }
        if ($user->hasRole('ROLE_CLINIC')) {
            $clinic = $this->clinicRepo->findByUser($user);
            return $clinic !== null ? [EntityInsurancePricing::TYPE_CLINIC, $clinic->getId()] : [EntityInsurancePricing::TYPE_CLINIC, null];
        }
        return ['unknown', null];
    }

    // ── Public list ───────────────────────────────────────────────────────────

    #[Route('/api/v1/insurances', methods: ['GET'])]
    public function list(Request $request): JsonResponse
    {
        $typeParam = $request->query->get('type');
        $type = null;
        if ($typeParam !== null) {
            $type = InsuranceType::tryFrom($typeParam);
        }

        $items = array_map(fn(Insurance $i) => $i->toArray(), $this->insuranceRepo->findActive($type));
        return $this->success(['data' => $items]);
    }

    // ── Admin CRUD — Insurance ────────────────────────────────────────────────

    #[Route('/api/v1/admin/insurance', methods: ['POST'])]
    #[IsGranted('ROLE_ADMIN')]
    public function create(Request $request): JsonResponse
    {
        $data = json_decode($request->getContent(), true) ?? [];
        $name = trim($data['name'] ?? '');
        $typeVal = $data['type'] ?? null;

        if ($name === '') {
            return $this->error(ErrorCodes::ERR_VALIDATION_001, 'name الزامی است', 422, 'name');
        }

        $type = InsuranceType::tryFrom((string) $typeVal);
        if ($type === null) {
            return $this->error(ErrorCodes::ERR_VALIDATION_001, 'type باید basic یا supplementary باشد', 422, 'type');
        }

        $insurance = new Insurance($name, $type);
        if (isset($data['logo_url'])) $insurance->setLogoUrl($data['logo_url']);
        if (isset($data['status']))   $insurance->setStatus((int) $data['status']);

        $this->insuranceRepo->save($insurance);
        return $this->success(['data' => $insurance->toArray()], 201);
    }

    #[Route('/api/v1/admin/insurance/{id}', methods: ['PATCH'])]
    #[IsGranted('ROLE_ADMIN')]
    public function update(int $id, Request $request): JsonResponse
    {
        $insurance = $this->insuranceRepo->find($id);
        if ($insurance === null) {
            return $this->error(ErrorCodes::ERR_VALIDATION_002, 'بیمه یافت نشد', 404);
        }

        $data = json_decode($request->getContent(), true) ?? [];
        if (isset($data['name'])) $insurance->setName($data['name']);
        if (isset($data['type'])) {
            $type = InsuranceType::tryFrom($data['type']);
            if ($type !== null) $insurance->setType($type);
        }
        if (array_key_exists('logo_url', $data)) $insurance->setLogoUrl($data['logo_url']);
        if (isset($data['status']))               $insurance->setStatus((int) $data['status']);

        $this->insuranceRepo->save($insurance);
        return $this->success(['data' => $insurance->toArray()]);
    }

    #[Route('/api/v1/admin/insurance/{id}', methods: ['DELETE'])]
    #[IsGranted('ROLE_ADMIN')]
    public function delete(int $id): JsonResponse
    {
        $insurance = $this->insuranceRepo->find($id);
        if ($insurance === null) {
            return $this->error(ErrorCodes::ERR_VALIDATION_002, 'بیمه یافت نشد', 404);
        }

        $this->insuranceRepo->remove($insurance);
        return $this->success(['message' => 'بیمه با موفقیت حذف شد']);
    }

    #[Route('/api/v1/admin/insurances', methods: ['GET'])]
    #[IsGranted('ROLE_ADMIN')]
    public function adminList(Request $request): JsonResponse
    {
        $page    = max(1, (int) $request->query->get('page', 1));
        $limit   = min(100, max(1, (int) $request->query->get('limit', 15)));
        $search  = trim((string) $request->query->get('search', ''));
        $typeParam = $request->query->get('type');

        $qb = $this->insuranceRepo->createQueryBuilder('i');
        if ($request->query->get('sort') === 'id') {
            $qb->orderBy('i.id', strtoupper((string) $request->query->get('order')) === 'DESC' ? 'DESC' : 'ASC');
        } else {
            $qb->orderBy('i.name', 'ASC');
        }

        if ($search !== '') {
            $qb->andWhere('i.name LIKE :s')->setParameter('s', '%' . $search . '%');
        }
        if ($typeParam !== null && $typeParam !== '') {
            $qb->andWhere('i.type = :t')->setParameter('t', $typeParam);
        }

        $total = (clone $qb)->select('COUNT(i.id)')->getQuery()->getSingleScalarResult();
        $rows  = $qb->setFirstResult(($page - 1) * $limit)->setMaxResults($limit)->getQuery()->getResult();

        return $this->paginated(
            array_map(fn(Insurance $i) => $i->toArray(), $rows),
            (int) $total, $page, $limit
        );
    }

    // ── Upload logo ───────────────────────────────────────────────────────────

    #[Route('/api/v1/admin/insurance/{id}/upload-logo', methods: ['POST'])]
    #[IsGranted('ROLE_ADMIN')]
    public function uploadLogo(int $id, Request $request): JsonResponse
    {
        $insurance = $this->insuranceRepo->find($id);
        if ($insurance === null) {
            return $this->error(ErrorCodes::ERR_VALIDATION_002, 'بیمه یافت نشد', 404);
        }

        $content     = $request->getContent();
        $disposition = $request->headers->get('Content-Disposition', '');
        preg_match('/filename=["\']?([^"\';\s]+)["\']?/i', $disposition, $m);
        $filename = $m[1] ?? 'logo.jpg';

        $tmpPath = sys_get_temp_dir() . '/' . uniqid('upload_', true);
        file_put_contents($tmpPath, $content);

        try {
            $safeFilename = $this->fileValidator->sanitizeFilename($filename);
            $mime         = $this->fileValidator->detectMimeType($tmpPath);

            $year  = date('Y');
            $month = date('m');
            $dir   = $this->projectDir . '/public/uploads/insurances/logo/' . $year . '-' . $month;
            if (!is_dir($dir)) mkdir($dir, 0755, true);

            $storedName = uniqid('', true) . '_' . $safeFilename;
            rename($tmpPath, $dir . '/' . $storedName);

            $url = '/uploads/insurances/logo/' . $year . '-' . $month . '/' . $storedName;
            $insurance->setLogoUrl($url);
            $this->insuranceRepo->save($insurance);

            return $this->success([
                'url'      => $url,
                'uuid'     => Uuid::v4()->toRfc4122(),
                'filename' => $safeFilename,
                'filemime' => $mime,
                'filesize' => strlen($content),
            ]);
        } catch (\Throwable $e) {
            if (file_exists($tmpPath)) unlink($tmpPath);
            return $this->error(ErrorCodes::ERR_VALIDATION_001, $e->getMessage(), 422);
        }
    }

    // ── Entity insurance pricing (visit price by insurance) ───────────────────

    #[Route('/api/v1/insurance-pricing', methods: ['GET'])]
    #[IsGranted('IS_AUTHENTICATED_FULLY')]
    public function getInsurancePricing(#[CurrentUser] User $user): JsonResponse
    {
        [$entityType, $entityId] = $this->resolveEntity($user);
        if ($entityId === null) {
            return $this->error(ErrorCodes::ERR_FORBIDDEN_001, 'پروفایل یافت نشد', 403);
        }

        $rows = $this->pricingRepo->findByEntity($entityType, $entityId);

        $freeVisitPriceRials = 0;
        $perInsurance = [];
        foreach ($rows as $row) {
            if ($row->isFreeVisit()) {
                $freeVisitPriceRials = $row->getPatientShareRials();
            } else {
                $perInsurance[$row->getInsuranceId()] = $row->getPatientShareRials();
            }
        }

        $insurances = array_map(function (Insurance $i) use ($perInsurance) {
            return [
                'insurance_id'        => $i->getId(),
                'insurance_name'      => $i->getName(),
                'type'                => $i->getType()->value,
                'patient_share_rials' => $perInsurance[$i->getId()] ?? null,
            ];
        }, $this->insuranceRepo->findActive(null));

        return $this->success([
            'entity_type'            => $entityType,
            'entity_id'              => $entityId,
            'free_visit_price_rials' => $freeVisitPriceRials,
            'insurances'             => $insurances,
        ]);
    }

    #[Route('/api/v1/insurance-pricing', methods: ['PUT'])]
    #[IsGranted('IS_AUTHENTICATED_FULLY')]
    public function saveInsurancePricing(Request $request, #[CurrentUser] User $user): JsonResponse
    {
        [$entityType, $entityId] = $this->resolveEntity($user);
        if ($entityId === null) {
            return $this->error(ErrorCodes::ERR_FORBIDDEN_001, 'پروفایل یافت نشد', 403);
        }

        $data = json_decode($request->getContent(), true) ?? [];

        if (array_key_exists('free_visit_price_rials', $data)) {
            $this->upsertPricing($entityType, $entityId, null, (int) $data['free_visit_price_rials']);
        }

        foreach (($data['insurances'] ?? []) as $row) {
            $insuranceId = isset($row['insurance_id']) ? (int) $row['insurance_id'] : null;
            if ($insuranceId === null) {
                continue;
            }
            if (!array_key_exists('patient_share_rials', $row) || $row['patient_share_rials'] === null) {
                $existing = $this->pricingRepo->findOneForInsurance($entityType, $entityId, $insuranceId);
                if ($existing !== null) {
                    $this->pricingRepo->remove($existing, false);
                }
                continue;
            }
            $this->upsertPricing($entityType, $entityId, $insuranceId, (int) $row['patient_share_rials']);
        }

        $this->pricingRepo->getEntityManager()->flush();

        return $this->getInsurancePricing($user);
    }

    private function upsertPricing(string $entityType, int $entityId, ?int $insuranceId, int $shareRials): void
    {
        $row = $this->pricingRepo->findOneForInsurance($entityType, $entityId, $insuranceId);
        if ($row === null) {
            $row = new EntityInsurancePricing($entityType, $entityId, $insuranceId, $shareRials);
        } else {
            $row->setPatientShareRials($shareRials);
        }
        $this->pricingRepo->save($row, false);
    }

    // ── TenantInsurance — قراردادهای بیمه‌ی tenant ─────────────────────────────

    #[Route('/api/v1/billing/tenant-insurances', methods: ['GET'])]
    #[IsGranted('IS_AUTHENTICATED_FULLY')]
    public function listTenantInsurances(#[CurrentUser] User $user): JsonResponse
    {
        [$entityType, $entityId] = $this->resolveEntity($user);
        if ($entityId === null) {
            return $this->error(ErrorCodes::ERR_FORBIDDEN_001, 'پروفایل یافت نشد', 403);
        }

        $contracts = $this->tenantInsuranceRepo->findActiveByTenant($entityType, $entityId);

        $byId = [];
        foreach ($this->insuranceRepo->findActive(null) as $ins) {
            $byId[$ins->getId()] = ['name' => $ins->getName(), 'type' => $ins->getType()->value];
        }

        $data = array_map(function (TenantInsurance $c) use ($byId) {
            $row = $c->toArray();
            $row['insurance_name'] = $byId[$c->getInsuranceId()]['name'] ?? null;
            $row['insurance_kind'] = $byId[$c->getInsuranceId()]['type'] ?? null;
            return $row;
        }, $contracts);

        return $this->success(['data' => $data]);
    }

    #[Route('/api/v1/billing/tenant-insurances', methods: ['POST'])]
    #[IsGranted('IS_AUTHENTICATED_FULLY')]
    public function activateTenantInsurance(Request $request, #[CurrentUser] User $user): JsonResponse
    {
        [$entityType, $entityId] = $this->resolveEntity($user);
        if ($entityId === null) {
            return $this->error(ErrorCodes::ERR_FORBIDDEN_001, 'پروفایل یافت نشد', 403);
        }

        $data        = json_decode($request->getContent(), true) ?? [];
        $insuranceId = isset($data['insurance_id']) ? (int) $data['insurance_id'] : 0;
        if ($insuranceId <= 0) {
            return $this->error(ErrorCodes::ERR_VALIDATION_001, 'insurance_id الزامی است', 422);
        }

        $contract = $this->tenantInsuranceService->activate(
            $entityType,
            $entityId,
            $insuranceId,
            (float) ($data['coverage_percent'] ?? 0),
            (int) ($data['franchise_rials'] ?? 0),
            isset($data['annual_ceiling_rials']) && $data['annual_ceiling_rials'] !== null
                ? (int) $data['annual_ceiling_rials'] : null,
        );

        return $this->success(['data' => $contract->toArray()], 201);
    }

    #[Route('/api/v1/billing/tenant-insurances/{uuid}', methods: ['PATCH'])]
    #[IsGranted('IS_AUTHENTICATED_FULLY')]
    public function updateTenantInsurance(string $uuid, Request $request, #[CurrentUser] User $user): JsonResponse
    {
        [$entityType, $entityId] = $this->resolveEntity($user);
        $contract = $this->tenantInsuranceRepo->findByUuid($uuid);
        if ($contract === null || $contract->getEntityType() !== $entityType || $contract->getEntityId() !== $entityId) {
            return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'قرارداد یافت نشد', 404);
        }

        $data = json_decode($request->getContent(), true) ?? [];
        if (array_key_exists('coverage_percent', $data)) {
            $contract->setCoveragePercent((float) $data['coverage_percent']);
        }
        if (array_key_exists('franchise_rials', $data)) {
            $contract->setFranchiseRials((int) $data['franchise_rials']);
        }
        if (array_key_exists('annual_ceiling_rials', $data)) {
            $contract->setAnnualCeilingRials($data['annual_ceiling_rials'] !== null ? (int) $data['annual_ceiling_rials'] : null);
        }

        $this->tenantInsuranceRepo->save($contract);

        return $this->success(['data' => $contract->toArray()]);
    }

    #[Route('/api/v1/billing/tenant-insurances/{uuid}', methods: ['DELETE'])]
    #[IsGranted('IS_AUTHENTICATED_FULLY')]
    public function deactivateTenantInsurance(string $uuid, #[CurrentUser] User $user): JsonResponse
    {
        [$entityType, $entityId] = $this->resolveEntity($user);
        $contract = $this->tenantInsuranceRepo->findByUuid($uuid);
        if ($contract === null || $contract->getEntityType() !== $entityType || $contract->getEntityId() !== $entityId) {
            return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'قرارداد یافت نشد', 404);
        }

        $this->tenantInsuranceService->deactivate($contract);

        return $this->success(['message' => 'قرارداد بیمه غیرفعال شد']);
    }

    #[Route('/api/v1/billing/tenant-insurances/{uuid}/service-coverage', methods: ['GET'])]
    #[IsGranted('IS_AUTHENTICATED_FULLY')]
    public function listServiceCoverage(string $uuid, #[CurrentUser] User $user): JsonResponse
    {
        [$entityType, $entityId] = $this->resolveEntity($user);
        $contract = $this->tenantInsuranceRepo->findByUuid($uuid);
        if ($contract === null || $contract->getEntityType() !== $entityType || $contract->getEntityId() !== $entityId) {
            return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'قرارداد یافت نشد', 404);
        }

        $rows = $this->serviceCoverageRepo->findByContract($contract->getId());

        // Batch-fetch the referenced service items once instead of one find()
        // per coverage row (N+1).
        $itemIds = array_values(array_unique(array_map(fn($r) => $r->getServiceItemId(), $rows)));
        $uuidById = [];
        if ($itemIds !== []) {
            foreach ($this->serviceItemRepo->findBy(['id' => $itemIds]) as $item) {
                $uuidById[$item->getId()] = $item->getUuid();
            }
        }

        $data = array_map(function ($r) use ($uuidById) {
            $row = $r->toArray();
            $row['service_item_uuid'] = $uuidById[$r->getServiceItemId()] ?? null;
            return $row;
        }, $rows);

        return $this->success(['data' => $data]);
    }

    #[Route('/api/v1/billing/tenant-insurances/{uuid}/service-coverage', methods: ['PUT'])]
    #[IsGranted('IS_AUTHENTICATED_FULLY')]
    public function setServiceCoverage(string $uuid, Request $request, #[CurrentUser] User $user): JsonResponse
    {
        [$entityType, $entityId] = $this->resolveEntity($user);
        $contract = $this->tenantInsuranceRepo->findByUuid($uuid);
        if ($contract === null || $contract->getEntityType() !== $entityType || $contract->getEntityId() !== $entityId) {
            return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'قرارداد یافت نشد', 404);
        }

        $data = json_decode($request->getContent(), true) ?? [];

        $serviceItem = isset($data['service_item_uuid'])
            ? $this->serviceItemRepo->findByUuid((string) $data['service_item_uuid'])
            : (isset($data['service_item_id']) ? $this->serviceItemRepo->find((int) $data['service_item_id']) : null);

        if ($serviceItem === null) {
            return $this->error(ErrorCodes::ERR_VALIDATION_001, 'سرویس یافت نشد', 422);
        }

        $section = $serviceItem->getSection();
        if ($section->getEntityType() !== $entityType || $section->getEntityId() !== $entityId) {
            return $this->error(ErrorCodes::ERR_FORBIDDEN_001, 'این سرویس متعلق به شما نیست', 403);
        }

        $serviceItemId = $serviceItem->getId();

        $this->tenantInsuranceService->setServiceCoverage(
            $contract,
            $serviceItemId,
            (bool) ($data['covered'] ?? true),
            isset($data['coverage_percent']) && $data['coverage_percent'] !== null ? (float) $data['coverage_percent'] : null,
            isset($data['franchise_rials']) && $data['franchise_rials'] !== null ? (int) $data['franchise_rials'] : null,
            isset($data['ceiling_rials']) && $data['ceiling_rials'] !== null ? (int) $data['ceiling_rials'] : null,
        );

        return $this->success(['message' => 'پوشش خدمت ذخیره شد']);
    }

    // ── DoctorInsurance CRUD ──────────────────────────────────────────────────

    #[Route('/api/v1/insurance/', methods: ['POST'])]
    #[IsGranted('IS_AUTHENTICATED_FULLY')]
    public function addDoctorInsurance(Request $request, #[CurrentUser] User $user): JsonResponse
    {
        $data        = json_decode($request->getContent(), true) ?? [];
        $doctorId    = $data['doctor_id'] ?? null;
        $insuranceId = $data['insurance_id'] ?? null;

        if (!$doctorId || !$insuranceId) {
            return $this->error(ErrorCodes::ERR_VALIDATION_001, 'doctor_id و insurance_id الزامی است', 422);
        }

        $doctor = $this->doctorRepo->find((int) $doctorId);
        if ($doctor === null) {
            return $this->error(ErrorCodes::ERR_VALIDATION_002, 'دکتر یافت نشد', 404);
        }

        if ($doctor->getUser()->getId() !== $user->getId() && !$user->hasRole('ROLE_ADMIN')) {
            return $this->error(ErrorCodes::ERR_AUTH_006, 'دسترسی ممنوع', 403);
        }

        $insurance = $this->insuranceRepo->find((int) $insuranceId);
        if ($insurance === null) {
            return $this->error(ErrorCodes::ERR_VALIDATION_002, 'بیمه یافت نشد', 404);
        }

        $existing = $this->doctorInsuranceRepo->findOneBy(['doctor' => $doctor, 'insurance' => $insurance]);
        if ($existing !== null) {
            return $this->error(ErrorCodes::ERR_VALIDATION_001, 'این بیمه قبلاً اضافه شده است', 409);
        }

        $doctorInsurance = new DoctorInsurance($doctor, $insurance);
        if (isset($data['price'])) {
            $doctorInsurance->setPrice((int) $data['price']);
        }

        $this->doctorInsuranceRepo->save($doctorInsurance);
        return $this->success(['data' => $doctorInsurance->toArray()], 201);
    }

    #[Route('/api/v1/insurance/{id}', methods: ['GET'])]
    #[IsGranted('IS_AUTHENTICATED_FULLY')]
    public function showDoctorInsurance(int $id, #[CurrentUser] User $user): JsonResponse
    {
        $doctorInsurance = $this->doctorInsuranceRepo->find($id);
        if ($doctorInsurance === null) {
            return $this->error(ErrorCodes::ERR_VALIDATION_002, 'بیمه پزشک یافت نشد', 404);
        }

        if ($doctorInsurance->getDoctor()->getUser()->getId() !== $user->getId() && !$user->hasRole('ROLE_ADMIN')) {
            return $this->error(ErrorCodes::ERR_AUTH_006, 'دسترسی ممنوع', 403);
        }

        return $this->success(['data' => $doctorInsurance->toArray()]);
    }

    #[Route('/api/v1/insurance/{id}', methods: ['PATCH'])]
    #[IsGranted('IS_AUTHENTICATED_FULLY')]
    public function updateDoctorInsurance(int $id, Request $request, #[CurrentUser] User $user): JsonResponse
    {
        $doctorInsurance = $this->doctorInsuranceRepo->find($id);
        if ($doctorInsurance === null) {
            return $this->error(ErrorCodes::ERR_VALIDATION_002, 'بیمه پزشک یافت نشد', 404);
        }

        if ($doctorInsurance->getDoctor()->getUser()->getId() !== $user->getId() && !$user->hasRole('ROLE_ADMIN')) {
            return $this->error(ErrorCodes::ERR_AUTH_006, 'دسترسی ممنوع', 403);
        }

        $data = json_decode($request->getContent(), true) ?? [];
        if (array_key_exists('price', $data)) {
            $doctorInsurance->setPrice($data['price'] !== null ? (int) $data['price'] : null);
        }

        $this->doctorInsuranceRepo->save($doctorInsurance);
        return $this->success(['data' => $doctorInsurance->toArray()]);
    }

    #[Route('/api/v1/insurance/{id}', methods: ['DELETE'])]
    #[IsGranted('IS_AUTHENTICATED_FULLY')]
    public function deleteDoctorInsurance(int $id, #[CurrentUser] User $user): JsonResponse
    {
        $doctorInsurance = $this->doctorInsuranceRepo->find($id);
        if ($doctorInsurance === null) {
            return $this->error(ErrorCodes::ERR_VALIDATION_002, 'بیمه پزشک یافت نشد', 404);
        }

        if ($doctorInsurance->getDoctor()->getUser()->getId() !== $user->getId() && !$user->hasRole('ROLE_ADMIN')) {
            return $this->error(ErrorCodes::ERR_AUTH_006, 'دسترسی ممنوع', 403);
        }

        $this->doctorInsuranceRepo->remove($doctorInsurance);
        return $this->success(['message' => 'بیمه پزشک با موفقیت حذف شد']);
    }
}
