<?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 AdminAppMerchandiseFormControllerTest 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/merchandise')
            ->assertUnauthorized();
    }

    public function test_it_returns_color_and_size_options_for_the_authenticated_tenant(): void
    {
        $tenant = $this->createTenant('fiesta');
        $otherTenant = $this->createTenant('other');
        $this->createAttribute($tenant, 'color', [
            ['value' => 'verde', 'label' => 'Verde', 'sort_order' => 2],
            ['value' => 'blanco', 'label' => 'Blanco', 'sort_order' => 1],
        ]);
        $this->createAttribute($tenant, 'talle', [
            ['value' => 'S', 'label' => 'S', 'sort_order' => 1],
            ['value' => 'M', 'label' => 'M', 'sort_order' => 2],
        ]);
        $this->createAttribute($otherTenant, 'color', [
            ['value' => 'negro', 'label' => 'Negro', '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/merchandise')
            ->assertOk()
            ->assertJsonCount(2, 'data.colors')
            ->assertJsonCount(2, 'data.sizes')
            ->assertJsonPath('data.colors.0.value', 'blanco')
            ->assertJsonPath('data.colors.0.label', 'Blanco')
            ->assertJsonPath('data.colors.1.value', 'verde')
            ->assertJsonPath('data.sizes.0.value', 'S')
            ->assertJsonPath('data.sizes.1.value', 'M')
            ->assertJsonMissing(['value' => 'negro']);
    }

    public function test_a_customer_cannot_get_the_merchandise_form(): void
    {
        Sanctum::actingAs(User::factory()->create([
            'rol_codigo' => RoleCode::User->value,
        ]));

        $this->getJson('/api/v1/adminapp/forms/fiesta-futbol-infantil/merchandise')
            ->assertForbidden();
    }

    private function createTenant(string $code): Tenant
    {
        return Tenant::query()->create([
            'codigo' => $code,
            'nombre' => ucfirst($code),
            'dominio' => "{$code}.test",
            'website_type_code' => 'onticket',
        ]);
    }

    /** @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);
    }
}
