<?php

namespace App\Tests\Appointment;

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

/**
 * عمومی: GET /api/v1/appointment-booking-services/{doctorUuid} روش نوبت‌دهی و
 * سرویس‌های bookable را بدون احراز هویت برمی‌گرداند (نوبت‌گیری آنلاین سرویسی).
 */
class BookingServicesPublicTest extends ApiTestCase
{
    public function testReturnsModeAndBookableServicesWithoutAuth(): 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);

        $bookable = (new ServiceItem($section, 'عصب‌کشی', 500000))->setDurationMinutes(30)->setBookable(true);
        $hidden   = (new ServiceItem($section, 'معاینه داخلی', 100000))->setDurationMinutes(15)->setBookable(false);
        $this->em->persist($bookable);
        $this->em->persist($hidden);

        $schedule = $this->newWeeklySchedule($doctor, []);
        $schedule->setMeta(['booking_mode' => 'service', 'buffer_minutes' => 5]);
        $this->em->persist($schedule);
        $this->em->flush();

        // بدون هدر Authorization
        $this->client->request('GET', '/api/v1/appointment-booking-services/' . $doctor->getUuid());
        $this->assertSame(200, $this->responseCode());

        $body = json_decode($this->client->getResponse()->getContent(), true);
        $data = $body['data'] ?? [];
        $this->assertSame('service', $data['booking_mode']);
        $this->assertSame(5, $data['buffer_minutes']);
        // فقط سرویس bookable برمی‌گردد
        $this->assertCount(1, $data['services']);
        $this->assertSame('عصب‌کشی', $data['services'][0]['name']);
        $this->assertSame(30, $data['services'][0]['duration_minutes']);
    }

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

        $this->client->request('GET', '/api/v1/appointment-booking-services/' . $doctor->getUuid());
        $this->assertSame(200, $this->responseCode());

        $data = json_decode($this->client->getResponse()->getContent(), true)['data'];
        $this->assertSame('slot', $data['booking_mode']);
        $this->assertSame([], $data['services']);
    }
}
