<?php

namespace Tests\Feature\Forms;

use App\Domains\Auth\Models\User;
use App\Domains\Authorization\Enums\RoleCode;
use App\Domains\Catalog\Models\Attribute;
use App\Domains\Shared\Enums\FieldType;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Tenant\Models\WebsiteType;
use Database\Seeders\AuthorizationSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class AdminAppFoodFormControllerTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed(AuthorizationSeeder::class);
        WebsiteType::query()->create(['codigo' => 'onticket', 'nombre' => 'OnTicket']);
    }

    public function test_authentication_is_required(): void
    {
        $this->getJson('/api/v1/adminapp/forms/fiesta-futbol-infantil/food')
            ->assertUnauthorized();
    }

    public function test_it_returns_event_dates_schedules_and_services_for_the_tenant(): void
    {
        $tenant = Tenant::query()->create([
            'codigo' => 'fiesta',
            'nombre' => 'Fiesta',
            'dominio' => 'fiesta.test',
            'website_type_code' => 'onticket',
        ]);
        $eventDate = $tenant->eventDates()->create([
            'date' => '2026-10-09',
            'time_start' => '00:00',
            'time_end' => '23:59',
        ]);
        $this->createAttribute($tenant, 'horario', [
            ['value' => 'Cena', 'label' => 'Cena', 'sort_order' => 2],
            ['value' => 'Almuerzo', 'label' => 'Almuerzo', 'sort_order' => 1],
        ]);
        $this->createAttribute($tenant, 'servicio', [
            ['value' => 'Comedor', 'label' => 'Comedor', 'sort_order' => 1],
        ]);

        Sanctum::actingAs(User::factory()->create([
            'rol_codigo' => RoleCode::AdminApp->value,
            'tenant_codigo' => $tenant->codigo,
        ]));

        $this->getJson('/api/v1/adminapp/forms/fiesta-futbol-infantil/food')
            ->assertOk()
            ->assertJsonCount(1, 'data.event_dates')
            ->assertJsonPath('data.event_dates.0.date', '2026-10-09')
            ->assertJsonPath('data.event_dates.0.validity_time_id', $eventDate->validity_time_id)
            ->assertJsonPath('data.event_dates.0.validity_time.type', 'fixed_window')
            ->assertJsonPath('data.schedules.0.value', 'Almuerzo')
            ->assertJsonPath('data.schedules.1.value', 'Cena')
            ->assertJsonPath('data.services.0.value', 'Comedor');
    }

    /** @param array<int, array{value: string, label: string, sort_order: int}> $options */
    private function createAttribute(Tenant $tenant, string $code, array $options): void
    {
        $attribute = Attribute::query()->create([
            'tenant_codigo' => $tenant->codigo,
            'codigo' => $code,
            'nombre' => ucfirst($code),
            'type' => FieldType::Select,
            'is_required' => true,
        ]);
        $attribute->options()->createMany($options);
    }
}
