<?php

namespace App\Tests\ClinicService;

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

/**
 * `service_category` classifies a service as outpatient/inpatient; the insurance
 * coverage percentage is resolved per that value. The category list is served by the
 * API so no client hardcodes the enum cases.
 */
class ServiceCategoryApiTest extends ApiTestCase
{
    /** @return array{0: \App\Auth\Entity\User, 1: ServiceSection} */
    private function makeDoctorAndSection(): array
    {
        $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();

        return [$owner, $section];
    }

    public function testCategoryListIsServedByTheApi(): void
    {
        $owner = $this->createUser(['ROLE_DOCTOR']);

        $body = $this->authJson('GET', '/api/v1/service-categories', $owner);

        self::assertSame(200, $this->responseCode());
        $keys = array_column($body['data']['data'], 'key');
        self::assertContains('outpatient', $keys);
        self::assertContains('inpatient', $keys);
    }

    public function testServiceDefaultsToOutpatient(): void
    {
        [$owner, $section] = $this->makeDoctorAndSection();

        $created = $this->authJson('POST', '/api/v1/service-item', $owner, [
            'section_uuid' => $section->getUuid(),
            'name'         => 'ویزیت سرپایی',
            'price_rials'  => 1_000,
        ]);

        self::assertSame(201, $this->responseCode());
        self::assertSame('outpatient', $created['data']['service_category']);
    }

    public function testCategoryIsStoredAndUpdatable(): void
    {
        [$owner, $section] = $this->makeDoctorAndSection();

        $created = $this->authJson('POST', '/api/v1/service-item', $owner, [
            'section_uuid'     => $section->getUuid(),
            'name'             => 'جراحی',
            'price_rials'      => 5_952_000,
            'service_category' => 'inpatient',
        ]);
        self::assertSame(201, $this->responseCode());
        self::assertSame('inpatient', $created['data']['service_category']);
        self::assertSame('خدمات بستری', $created['data']['service_category_label']);

        $updated = $this->authJson('PATCH', '/api/v1/service-item/' . $created['data']['uuid'], $owner, [
            'service_category' => 'outpatient',
        ]);
        self::assertSame(200, $this->responseCode());
        self::assertSame('outpatient', $updated['data']['service_category']);
    }

    public function testInvalidCategoryIsRejected(): void
    {
        [$owner, $section] = $this->makeDoctorAndSection();

        $this->authJson('POST', '/api/v1/service-item', $owner, [
            'section_uuid'     => $section->getUuid(),
            'name'             => 'خدمت نامعتبر',
            'service_category' => 'dental',
        ]);

        self::assertSame(422, $this->responseCode());
    }
}
