<?php

namespace Tests\Feature;

use App\Enums\CustomFieldType;
use App\Enums\EventStatus;
use App\Models\Category;
use App\Models\Event;
use App\Models\Organizer;
use App\Models\Role;
use App\Models\User;
use App\Models\Venue;
use Database\Seeders\DemoCatalogSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Support\FullTestEventStorePayload;
use Tests\TestCase;

/**
 * Registra un evento de prueba con el máximo de campos del formulario (HTTP store)
 * para detectar regresiones en validación y persistencia.
 */
class FullEventRegressionTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed(DemoCatalogSeeder::class);
    }

    public function test_admin_can_store_full_event_with_ticket_types_and_custom_fields(): void
    {
        $admin = User::factory()->administrador()->create();
        $venue = Venue::query()->firstOrFail();
        $category = Category::query()->firstOrFail();

        $payload = FullTestEventStorePayload::forStore($venue, $category);

        $response = $this->actingAs($admin)->post(route('dashboard.events.store'), $payload);

        $response->assertRedirect();

        $this->assertDatabaseHas('events', [
            'title' => 'PRUEBA REGITICKET · Evento completo',
            'slug' => $payload['slug'],
            'status' => EventStatus::Published->value,
            'hide_from_listings' => 1,
            'sticky_in_calendar' => 1,
            'is_featured' => 1,
        ]);

        $event = Event::query()->where('slug', $payload['slug'])->firstOrFail();
        $this->assertSame(3, $event->ticketTypes()->count());

        $general = $event->ticketTypes()->where('name', 'General early bird')->firstOrFail();
        $this->assertSame('35.00', (string) $general->sale_price);
        $this->assertDatabaseHas('custom_fields', [
            'ticket_type_id' => $general->id,
            'label' => 'Talla de camiseta',
            'type' => CustomFieldType::Select->value,
        ]);
        $this->assertDatabaseHas('custom_fields', [
            'ticket_type_id' => $general->id,
            'label' => 'Notas alergias',
            'type' => CustomFieldType::Textarea->value,
        ]);

        $vip = $event->ticketTypes()->where('name', 'VIP pool compartido')->firstOrFail();
        $this->assertSame(800, $vip->shared_pool_size);

        $this->actingAs($admin)
            ->get(route('dashboard.events.edit', $event))
            ->assertOk()
            ->assertSee('PRUEBA REGITICKET', false);
    }

    public function test_organizador_store_full_payload_stays_pending_but_persists_tickets(): void
    {
        $user = User::factory()->organizador()->create();
        Organizer::query()->create(['user_id' => $user->id, 'stripe_account_id' => null]);
        $venue = Venue::query()->firstOrFail();
        $category = Category::query()->firstOrFail();

        $payload = FullTestEventStorePayload::forStore($venue, $category);
        unset($payload['status']);

        $response = $this->actingAs($user)->post(route('dashboard.events.store'), $payload);

        $response->assertRedirect();

        $this->assertDatabaseHas('events', [
            'slug' => $payload['slug'],
            'status' => EventStatus::Pending->value,
        ]);

        $event = Event::query()->where('slug', $payload['slug'])->firstOrFail();
        $this->assertSame(3, $event->ticketTypes()->count());
    }
}
