<?php

namespace App\Tests\ClinicService;

use App\ClinicService\Entity\ServiceItem;
use App\ClinicService\Entity\ServiceSection;
use App\Doctor\Entity\Doctor;
use App\Tests\ApiTestCase;

/**
 * سرویس‌ها اصلاً حذف نمی‌شوند — نوبت/جلسه/فاکتور/سوابق پرداخت به سرویس ارجاع
 * دارند. DELETE با ۴۰۹ رد می‌شود و سرویس دست‌نخورده می‌ماند؛ فقط غیرفعال‌کردن
 * (PATCH active=false) مجاز است.
 */
class ServiceItemDeleteCleanupTest extends ApiTestCase
{
    public function testDeletingItemIsRejected(): void
    {
        $owner  = $this->createUser(['ROLE_DOCTOR']);
        $doctor = new Doctor($owner, 'دکتر');
        $this->em->persist($doctor);
        $this->em->flush();

        $section = new ServiceSection('doctor', $doctor->getId(), 'بخش');
        $item    = new ServiceItem($section, 'خدمت');
        $this->em->persist($section);
        $this->em->persist($item);
        $this->em->flush();
        $itemUuid = $item->getUuid();

        $this->authJson('DELETE', '/api/v1/service-item/' . $itemUuid, $owner);
        $this->assertSame(409, $this->responseCode());

        // سرویس باید همچنان وجود داشته باشد.
        $this->em->clear();
        $this->assertNotNull($this->em->getRepository(ServiceItem::class)->findOneBy(['uuid' => $itemUuid]));
    }

    public function testDeletingSectionIsRejected(): void
    {
        $owner  = $this->createUser(['ROLE_DOCTOR']);
        $doctor = new Doctor($owner, 'دکتر');
        $this->em->persist($doctor);
        $this->em->flush();

        $section = new ServiceSection('doctor', $doctor->getId(), 'بخش');
        $this->em->persist($section);
        $this->em->flush();
        $sectionUuid = $section->getUuid();

        $this->authJson('DELETE', '/api/v1/service-section/' . $sectionUuid, $owner);
        $this->assertSame(409, $this->responseCode());

        $this->em->clear();
        $this->assertNotNull($this->em->getRepository(ServiceSection::class)->findOneBy(['uuid' => $sectionUuid]));
    }
}
