<?php

namespace Database\Seeders;

use App\Enums\ChampionshipMatchStageEnum;
use App\Enums\ChampionshipMatchStatusEnum;
use App\Enums\ChampionshipStatusEnum;
use App\Enums\TeamMemberInvitationStatusEnum;
use App\Models\Championship;
use App\Models\ChampionshipMatch;
use App\Models\ChampionshipTeam;
use App\Models\Court;
use App\Models\Game;
use App\Models\Member;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class ChampionshipSeeder extends Seeder
{
    public function run(): void
    {
        $game = Game::where('id', 6)->first();
        if (! $game) {
            return;
        }

        $court = Court::where('game_id', $game->id)->first();

        DB::transaction(function () use ($game) {

            $oldChampionship = Championship::first();
            if ($oldChampionship) {
                $oldChampionship->delete();
            }

            $championship = Championship::create([
                'game_id' => $game->id,
                'name' => [
                    'en' => 'Champions League 2026',
                    'ar' => 'دوري أبطال أوروبا 2026',
                ],
                'image' => 'championships/1.png',
                'price' => 200.00,
                'players_count' => 11,
                'status' => ChampionshipStatusEnum::ONGOING->value,
            ]);

            $membersCount = Member::count();
            if ($membersCount < 88) {
                $toCreate = 88 - $membersCount;
                for ($i = 0; $i < $toCreate; $i++) {
                    Member::create([
                        'name' => 'Player Test '.($i + 1),
                        'phone' => '1000000'.rand(10, 99),
                        'email' => 'player'.rand(1000, 9999).'@mont.test',
                        'password' => bcrypt('password'),
                        'status' => 2,
                        'member_type' => 1,
                    ]);
                }
            }

            $members = Member::inRandomOrder()->take(88)->get();

            $teams = collect();
            $teamNames = [
                'Real Madrid', 'Barcelona', 'Manchester United', 'Bayern Munich',
                'Juventus', 'Liverpool', 'PSG', 'Chelsea',
            ];

            foreach ($teamNames as $index => $name) {
                $creator = $members[$index * 11];
                $team = ChampionshipTeam::create([
                    'championship_id' => $championship->id,
                    'name' => $name,
                    'created_by_member_id' => $creator->id,
                    'logo' => 'championship-teams/'.rand(1, 3).'.png',
                    'is_confirmed' => true,
                ]);

                $memberIds = $members->slice($index * 11, 11)->pluck('id')->all();
                $syncPayload = [];
                foreach ($memberIds as $mid) {
                    $syncPayload[$mid] = [
                        'status' => TeamMemberInvitationStatusEnum::ACCEPTED->value,
                        'invited_at' => now(),
                        'responded_at' => now(),
                    ];
                }
                $team->members()->sync($syncPayload);
                $teams->push($team);
            }

            // ربع نهائي
            // $quarterMatches = [];
            // $pairs = [[0,1],[2,3],[4,5],[6,7]];
            // foreach ($pairs as $i => $pair) {
            //     $match = ChampionshipMatch::create([
            //         'championship_id' => $championship->id,
            //         'team_a_id' => $teams[$pair[0]]->id,
            //         'team_b_id' => $teams[$pair[1]]->id,
            //         'court_id' => 1,
            //         'match_date' => now()->addDays($i + 1)->toDateString(),
            //         'start_time' => '18:00',
            //         'end_time' => '19:30',
            //         'stage' => ChampionshipMatchStageEnum::QUARTER_FINAL->value,
            //         'status' => ChampionshipMatchStatusEnum::SCHEDULED->value,
            //         'round' => 'Quarter ' . ($i + 1),
            //         'sort_order' => $i + 1,
            //     ]);
            //     $quarterMatches[] = $match;
            // }

            // // نصف نهائي
            // $semi1 = ChampionshipMatch::create([
            //     'championship_id' => $championship->id,
            //     'parent_match_id' => null,
            //     'team_a_id' => $quarterMatches[0]->team_a_id,
            //     'team_b_id' => $quarterMatches[1]->team_a_id,
            //     'court_id' => 1,
            //     'match_date' => now()->addDays(7)->toDateString(),
            //     'start_time' => '20:00',
            //     'end_time' => '21:30',
            //     'stage' => ChampionshipMatchStageEnum::SEMI_FINAL->value,
            //     'status' => ChampionshipMatchStatusEnum::SCHEDULED->value,
            //     'round' => 'Semi 1',
            //     'sort_order' => 5,
            // ]);

            // $semi2 = ChampionshipMatch::create([
            //     'championship_id' => $championship->id,
            //     'parent_match_id' => null,
            //     'team_a_id' => $quarterMatches[2]->team_a_id,
            //     'team_b_id' => $quarterMatches[3]->team_a_id,
            //     'court_id' => 1,
            //     'match_date' => now()->addDays(8)->toDateString(),
            //     'start_time' => '20:00',
            //     'end_time' => '21:30',
            //     'stage' => ChampionshipMatchStageEnum::SEMI_FINAL->value,
            //     'status' => ChampionshipMatchStatusEnum::SCHEDULED->value,
            //     'round' => 'Semi 2',
            //     'sort_order' => 6,
            // ]);

            // // النهائي
            // $final = ChampionshipMatch::create([
            //     'championship_id' => $championship->id,
            //     'parent_match_id' => null,
            //     'court_id' => 1,
            //     'match_date' => now()->addDays(14)->toDateString(),
            //     'start_time' => '20:00',
            //     'end_time' => '21:30',
            //     'stage' => ChampionshipMatchStageEnum::FINAL->value,
            //     'status' => ChampionshipMatchStatusEnum::SCHEDULED->value,
            //     'round' => 'Final',
            //     'sort_order' => 7,
            // ]);

            // $this->command->info('Professional Champions League seeded successfully.');
        });
    }
}
