<?php

namespace Database\Seeders;

use App\Enums\CustomFieldType;
use App\Enums\EventStatus;
use App\Enums\TicketCapacityStrategy;
use App\Enums\TicketIac;
use App\Models\Category;
use App\Models\Event;
use App\Models\Organizer;
use App\Models\Role;
use App\Models\TicketType;
use App\Models\User;
use App\Models\Venue;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Schema;

/**
 * Evento de demostración con varias entradas y campo personalizado (datos directos en BD).
 *
 * Uso: `php artisan db:seed --class=DemoFullEventSeeder`
 * Requiere catálogo (sedes/categorías), p. ej. tras {@see DemoCatalogSeeder}.
 */
class DemoFullEventSeeder extends Seeder
{
    private const SLUG = 'regiticket-demo-completo-seed';

    public function run(): void
    {
        $this->call(DemoCatalogSeeder::class);

        $venue = Venue::query()->where('slug', 'sede-principal')->firstOrFail();
        $category = Category::query()->where('slug', 'general')->firstOrFail();

        $user = User::query()->firstOrCreate(
            ['email' => 'demo-evento-completo@regiticket.local'],
            [
                'name' => 'Organizador demo (evento completo)',
                'password' => 'password',
                'role_id' => Role::query()->where('slug', Role::SLUG_ORGANIZADOR)->value('id'),
            ]
        );

        $organizer = Organizer::query()->firstOrCreate(
            ['user_id' => $user->id],
            ['stripe_account_id' => null]
        );

        Event::query()->where('slug', self::SLUG)->delete();

        $start = now()->addMonths(2)->startOfHour();
        $end = $start->copy()->addHours(6);

        $eventAttrs = [
            'organizer_id' => $organizer->id,
            'venue_id' => $venue->id,
            'category_id' => $category->id,
            'title' => 'Evento demo completo (seeder)',
            'description' => '<p>Generado por <code>DemoFullEventSeeder</code>.</p>',
            'short_description' => 'Carrera / evento de prueba con entradas variadas.',
            'start_date' => $start,
            'end_date' => $end,
            'timezone' => 'America/Puerto_Rico',
            'status' => EventStatus::Published,
        ];

        if (Schema::hasColumn('events', 'hide_from_listings')) {
            $eventAttrs['hide_from_listings'] = false;
            $eventAttrs['sticky_in_calendar'] = true;
            $eventAttrs['is_featured'] = true;
        }

        $event = Event::query()->create(array_merge($eventAttrs, ['slug' => self::SLUG]));

        $general = TicketType::query()->create([
            'event_id' => $event->id,
            'name' => 'General',
            'description' => 'Entrada general con cupo.',
            'price' => 40.00,
            'sale_price' => 30.00,
            'currency' => 'USD',
            'sku' => 'DEMO-GEN',
            'capacity_strategy' => TicketCapacityStrategy::PerTicket,
            'capacity' => 1000,
            'shared_pool_size' => null,
            'quantity_sold' => 0,
            'is_active' => true,
            'sale_start' => null,
            'sale_end' => null,
            'iac' => TicketIac::Required,
            'ticket_kind' => 'standard',
        ]);

        $general->customFields()->create([
            'label' => 'Talla (seeder)',
            'slug' => 'talla-seeder',
            'type' => CustomFieldType::Select,
            'options' => ['S', 'M', 'L', 'XL'],
            'required' => true,
            'sort_order' => 0,
        ]);

        TicketType::query()->create([
            'event_id' => $event->id,
            'name' => 'VIP',
            'description' => null,
            'price' => 100.00,
            'sale_price' => null,
            'currency' => 'USD',
            'sku' => 'DEMO-VIP',
            'capacity_strategy' => TicketCapacityStrategy::Unlimited,
            'capacity' => 0,
            'shared_pool_size' => null,
            'quantity_sold' => 0,
            'is_active' => true,
            'sale_start' => null,
            'sale_end' => null,
            'iac' => TicketIac::Optional,
            'ticket_kind' => 'standard',
        ]);
    }
}
