<?php



namespace Database\Seeders;



use App\Enums\ApprovalMode;

use App\Enums\EventStatus;

use App\Enums\EventType;

use App\Enums\LocationType;

use App\Enums\ParticipationType;

use App\Enums\PricingType;

use App\Models\Event;

use App\Models\EventPaymentMethod;

use App\Models\EventPrize;

use App\Models\EventRule;

use App\Models\Game;

use App\Models\ManualPaymentMethod;

use App\Models\User;

use Illuminate\Database\Seeder;



class EventSeeder extends Seeder

{

    public function run(): void

    {

        $admin = User::query()->where('email', 'admin@algaarena.gg')->first();

        $game = Game::query()->where('slug', 'valorant')->first()

            ?? Game::query()->first();



        if (! $admin || ! $game) {

            return;

        }



        $freeEvent = $this->seedFreeOpenEvent($admin, $game);

        $paidEvent = $this->seedPaidLeagueEvent($admin, $game);

        $this->seedWaitlistEvent($admin, $game);

        $this->seedAdminReviewEvent($admin, $game);

        $this->seedDraftEvent($admin, $game);

        $this->seedCompletedEvent($admin, $game);



        ManualPaymentMethod::query()->where('is_active', true)->each(function (ManualPaymentMethod $method) use ($paidEvent) {

            EventPaymentMethod::updateOrCreate(

                [

                    'event_id' => $paidEvent->id,

                    'manual_payment_method_id' => $method->id,

                ],

                ['is_active' => true],

            );

        });

    }



    private function seedFreeOpenEvent(User $admin, Game $game): Event

    {

        $event = Event::updateOrCreate(

            ['slug' => 'alga-open-scrim-cup'],

            [

                'game_id' => $game->id,

                'created_by' => $admin->id,

                'title' => 'ALGA Open Scrim Cup',

                'short_description' => 'Free community tournament for registered players.',

                'description' => 'Join the ALGA Open Scrim Cup and compete with players in your region. Automatic confirmation after registration.',

                'cover_image' => '/assets/images/generated/tournaments/tournament-cover-1.webp',

                'event_type' => EventType::Tournament,

                'participation_type' => ParticipationType::Individual,

                'location_type' => LocationType::Online,

                'online_details' => 'Discord lobby link sent after confirmation.',

                'timezone' => 'Africa/Cairo',

                'registration_opens_at' => now()->subDay(),

                'registration_closes_at' => now()->addWeeks(2),

                'starts_at' => now()->addWeeks(3),

                'ends_at' => now()->addWeeks(3)->addDays(2),

                'capacity' => 32,

                'waitlist_enabled' => true,

                'approval_mode' => ApprovalMode::Automatic,

                'pricing_type' => PricingType::Free,

                'rules' => 'Standard competitive rules apply. Screenshot evidence required for disputes.',

                'prizes' => '1st: 500 USD · 2nd: 250 USD · 3rd: 100 USD',

                'contact_information' => 'events@algaarena.gg',

                'status' => EventStatus::RegistrationOpen,

                'published_at' => now()->subDays(2),

            ],

        );



        EventPrize::updateOrCreate(

            ['event_id' => $event->id, 'placement' => '1st'],

            ['amount' => '500 USD', 'description' => 'Champion prize', 'sort_order' => 1],

        );



        EventPrize::updateOrCreate(

            ['event_id' => $event->id, 'placement' => '2nd'],

            ['amount' => '250 USD', 'description' => 'Runner-up prize', 'sort_order' => 2],

        );



        EventRule::updateOrCreate(

            ['event_id' => $event->id, 'title' => 'Eligibility'],

            ['content' => 'Players must have a verified game profile for the event game.', 'sort_order' => 1],

        );



        return $event;

    }



    private function seedPaidLeagueEvent(User $admin, Game $game): Event

    {

        $event = Event::updateOrCreate(

            ['slug' => 'alga-premier-league'],

            [

                'game_id' => $game->id,

                'created_by' => $admin->id,

                'title' => 'ALGA Premier League',

                'short_description' => 'Paid league entry with manual payment verification.',

                'description' => 'Register for the ALGA Premier League. Complete manual payment and upload proof for admin review.',

                'cover_image' => '/assets/images/generated/tournaments/tournament-cover-2.webp',

                'event_type' => EventType::TeamCompetition,

                'participation_type' => ParticipationType::Team,

                'location_type' => LocationType::Online,

                'timezone' => 'Africa/Cairo',

                'registration_opens_at' => now()->subDay(),

                'registration_closes_at' => now()->addWeeks(3),

                'starts_at' => now()->addMonth(),

                'ends_at' => now()->addMonth()->addDays(5),

                'capacity' => 16,

                'waitlist_enabled' => true,

                'approval_mode' => ApprovalMode::Automatic,

                'pricing_type' => PricingType::Paid,

                'fee_amount_minor' => 25000,

                'currency' => 'EGP',

                'payment_instructions' => 'Pay using one of the enabled methods, then upload your receipt on the registration page.',

                'rules' => 'Team rosters locked after payment confirmation.',

                'prizes' => '1st: 10000 EGP · 2nd: 5000 EGP',

                'contact_information' => 'payments@algaarena.gg',

                'status' => EventStatus::RegistrationOpen,

                'published_at' => now()->subDay(),

            ],

        );



        EventPrize::updateOrCreate(

            ['event_id' => $event->id, 'placement' => '1st'],

            ['amount' => '10000 EGP', 'description' => 'Grand prize', 'sort_order' => 1],

        );



        return $event;

    }



    private function seedWaitlistEvent(User $admin, Game $game): void

    {

        Event::updateOrCreate(

            ['slug' => 'alga-quick-cup'],

            [

                'game_id' => $game->id,

                'created_by' => $admin->id,

                'title' => 'ALGA Quick Cup',

                'short_description' => 'Small capacity event demonstrating waitlist flow.',

                'description' => 'Limited to 2 confirmed slots with waitlist enabled for overflow registrations.',

                'cover_image' => '/assets/images/generated/heroes/hero-arena.webp',

                'event_type' => EventType::CommunityEvent,

                'participation_type' => ParticipationType::Individual,

                'location_type' => LocationType::Online,

                'timezone' => 'Africa/Cairo',

                'registration_opens_at' => now()->subHours(12),

                'registration_closes_at' => now()->addWeek(),

                'starts_at' => now()->addWeeks(2),

                'ends_at' => now()->addWeeks(2)->addDay(),

                'capacity' => 2,

                'waitlist_enabled' => true,

                'approval_mode' => ApprovalMode::Automatic,

                'pricing_type' => PricingType::Free,

                'status' => EventStatus::RegistrationOpen,

                'published_at' => now()->subHours(12),

            ],

        );

    }



    private function seedAdminReviewEvent(User $admin, Game $game): void

    {

        Event::updateOrCreate(

            ['slug' => 'alga-community-qualifier'],

            [

                'game_id' => $game->id,

                'created_by' => $admin->id,

                'title' => 'ALGA Community Qualifier',

                'short_description' => 'Free event requiring admin approval before confirmation.',

                'description' => 'Registrations remain pending until an administrator reviews and approves each entry.',

                'cover_image' => '/assets/images/generated/tournaments/tournament-cover-1.webp',

                'event_type' => EventType::Qualifier,

                'participation_type' => ParticipationType::Individual,

                'location_type' => LocationType::Online,

                'timezone' => 'Africa/Cairo',

                'registration_opens_at' => now()->subDay(),

                'registration_closes_at' => now()->addWeeks(2),

                'starts_at' => now()->addWeeks(4),

                'ends_at' => now()->addWeeks(4)->addDays(1),

                'capacity' => 24,

                'waitlist_enabled' => false,

                'approval_mode' => ApprovalMode::AdminReview,

                'pricing_type' => PricingType::Free,

                'status' => EventStatus::RegistrationOpen,

                'published_at' => now()->subDay(),

            ],

        );

    }



    private function seedDraftEvent(User $admin, Game $game): void

    {

        Event::updateOrCreate(

            ['slug' => 'alga-invite-only-draft'],

            [

                'game_id' => $game->id,

                'created_by' => $admin->id,

                'title' => 'ALGA Invite Series (Draft)',

                'short_description' => 'Draft event — not publicly visible until published.',

                'description' => 'Internal draft for upcoming invite-only series.',

                'event_type' => EventType::Tournament,

                'participation_type' => ParticipationType::Individual,

                'location_type' => LocationType::Online,

                'timezone' => 'Africa/Cairo',

                'registration_opens_at' => now()->addWeek(),

                'registration_closes_at' => now()->addWeeks(3),

                'starts_at' => now()->addMonth(),

                'ends_at' => now()->addMonth()->addDays(3),

                'capacity' => 8,

                'waitlist_enabled' => false,

                'approval_mode' => ApprovalMode::Automatic,

                'pricing_type' => PricingType::Free,

                'status' => EventStatus::Draft,

                'published_at' => null,

            ],

        );

    }



    private function seedCompletedEvent(User $admin, Game $game): void

    {

        Event::updateOrCreate(

            ['slug' => 'alga-winter-showcase-2025'],

            [

                'game_id' => $game->id,

                'created_by' => $admin->id,

                'title' => 'ALGA Winter Showcase 2025',

                'short_description' => 'Completed showcase event from previous season.',

                'description' => 'Archive event used for admin metrics and historical registration examples.',

                'cover_image' => '/assets/images/generated/tournaments/tournament-cover-2.webp',

                'event_type' => EventType::Tournament,

                'participation_type' => ParticipationType::Individual,

                'location_type' => LocationType::Online,

                'timezone' => 'Africa/Cairo',

                'registration_opens_at' => now()->subMonths(3),

                'registration_closes_at' => now()->subMonths(2),

                'starts_at' => now()->subMonths(2),

                'ends_at' => now()->subMonths(2)->addDays(3),

                'capacity' => 16,

                'waitlist_enabled' => false,

                'approval_mode' => ApprovalMode::Automatic,

                'pricing_type' => PricingType::Free,

                'status' => EventStatus::Completed,

                'published_at' => now()->subMonths(3),

            ],

        );

    }

}


