<?php

namespace App\Tests\Insurance;

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

/**
 * The tenant-wide insurance service kinds travel on the existing insurance-pricing
 * endpoint — it already carries the tenant's global insurance settings.
 */
class InsuranceServiceCategorySettingsApiTest extends ApiTestCase
{
    private function makeDoctor(): \App\Auth\Entity\User
    {
        $owner  = $this->createUser(['ROLE_DOCTOR']);
        $doctor = new Doctor($owner, 'دکتر تست نوع خدمات بیمه');
        $this->em->persist($doctor);
        $this->em->flush();

        return $owner;
    }

    public function testPricingPayloadCarriesEveryServiceCategory(): void
    {
        $owner = $this->makeDoctor();

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

        $this->assertSame(200, $this->responseCode());
        $rows = $body['data']['service_categories'];
        $this->assertSame(['outpatient', 'inpatient'], array_column($rows, 'key'));
        $this->assertSame([true, true], array_column($rows, 'enabled'));
        // هر دو فعال → پنل باید بپرسد.
        $this->assertNull($body['data']['default_service_category']);
    }

    public function testDisablingOneKindMakesTheOtherTheDefault(): void
    {
        $owner = $this->makeDoctor();

        $body = $this->authJson('PUT', '/api/v1/insurance-pricing', $owner, [
            'service_categories' => [['key' => 'inpatient', 'enabled' => false]],
        ]);

        $this->assertSame(200, $this->responseCode());
        $this->assertSame('outpatient', $body['data']['default_service_category']);
        $enabled = array_column($body['data']['service_categories'], 'enabled', 'key');
        $this->assertFalse($enabled['inpatient']);
        $this->assertTrue($enabled['outpatient']);
    }

    public function testDisablingEveryKindIsRejected(): void
    {
        $owner = $this->makeDoctor();

        $this->authJson('PUT', '/api/v1/insurance-pricing', $owner, [
            'service_categories' => [
                ['key' => 'outpatient', 'enabled' => false],
                ['key' => 'inpatient',  'enabled' => false],
            ],
        ]);

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

    public function testUnknownKindIsRejected(): void
    {
        $owner = $this->makeDoctor();

        $this->authJson('PUT', '/api/v1/insurance-pricing', $owner, [
            'service_categories' => [['key' => 'dental', 'enabled' => true]],
        ]);

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

    /** بدون کلید `service_categories`، تنظیمات دست‌نخورده می‌ماند. */
    public function testOmittingTheKeyKeepsTheStoredSettings(): void
    {
        $owner = $this->makeDoctor();
        $this->authJson('PUT', '/api/v1/insurance-pricing', $owner, [
            'service_categories' => [['key' => 'inpatient', 'enabled' => false]],
        ]);

        $body = $this->authJson('PUT', '/api/v1/insurance-pricing', $owner, ['free_visit_price_rials' => 5_000_000]);

        $this->assertSame(200, $this->responseCode());
        $this->assertSame('outpatient', $body['data']['default_service_category']);
        $this->assertSame(5_000_000, $body['data']['free_visit_price_rials']);
    }
}
