<?php

namespace App\Tests\ClinicService;

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

/**
 * GET /api/v1/service-item/{uuid} backs the dedicated service detail page, which
 * must survive a hard refresh — hence a single-item fetch instead of filtering
 * the full list client-side.
 */
class ServiceItemDetailApiTest extends ApiTestCase
{
    /** @return array{0: User, 1: ServiceItem} */
    private function makeDoctorWithItem(string $sectionName = 'تزریقات'): array
    {
        $owner  = $this->createUser(['ROLE_DOCTOR']);
        $doctor = new Doctor($owner, 'دکتر تست جزئیات');
        $this->em->persist($doctor);
        $this->em->flush();

        $section = new ServiceSection('doctor', $doctor->getId(), $sectionName);
        $item    = new ServiceItem($section, 'سرم ۵۰۰cc');
        $item->setPriceRials(850_000)->setDurationMinutes(30)->setBookable(true);
        $this->em->persist($section);
        $this->em->persist($item);
        $this->em->flush();

        return [$owner, $item];
    }

    public function testReturnsTheItemWithEverythingTheDetailPageNeeds(): void
    {
        [$owner, $item] = $this->makeDoctorWithItem();

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

        $this->assertSame(200, $this->responseCode());
        $row = $body['data'];
        $this->assertSame($item->getUuid(), $row['uuid']);
        $this->assertSame('سرم ۵۰۰cc', $row['name']);
        $this->assertSame('تزریقات', $row['section_name'], 'breadcrumb به نام بخش نیاز دارد');
        $this->assertSame($item->getSection()->getUuid(), $row['section_uuid']);
        $this->assertSame(850_000, $row['price_rials']);
        $this->assertSame(30, $row['duration_minutes']);
        $this->assertTrue($row['bookable']);
        $this->assertIsInt($row['created_at']);
        $this->assertIsInt($row['updated_at']);
        $this->assertArrayHasKey('staff_members', $row);
        $this->assertArrayHasKey('insurance_covered', $row);
    }

    public function testUnknownUuidReturns404(): void
    {
        [$owner] = $this->makeDoctorWithItem();

        $this->authJson('GET', '/api/v1/service-item/00000000-0000-4000-8000-000000000000', $owner);

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

    public function testAnotherTenantCannotReadTheItem(): void
    {
        [, $item] = $this->makeDoctorWithItem();

        $stranger = $this->createUser(['ROLE_DOCTOR']);
        $this->em->persist(new Doctor($stranger, 'دکتر غریبه'));
        $this->em->flush();

        $this->authJson('GET', '/api/v1/service-item/' . $item->getUuid(), $stranger);

        $this->assertSame(404, $this->responseCode(), 'وجود سرویس نباید به tenant دیگر لو برود');
    }

    public function testRequiresAuthentication(): void
    {
        [, $item] = $this->makeDoctorWithItem();

        $this->client->request('GET', '/api/v1/service-item/' . $item->getUuid());

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