<?php

namespace Database\Seeders;

use App\Models\GameMatch;
use App\Models\Season;
use App\Models\User;
use App\Services\EloService;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // Create Season 1
        $season = Season::create([
            'name'       => 'Season 1',
            'start_date' => now()->startOfYear(),
            'end_date'   => now()->endOfYear(),
            'is_active'  => true,
        ]);

        // Create sample players with varied ELOs
        $playerData = [
            ['Andi', 1850], ['Budi', 1720], ['Candra', 1650], ['Dedi', 1580],
            ['Eko', 1490],  ['Fajar', 1420],['Gilang', 1350], ['Hadi', 1280],
            ['Irfan', 1210],['Joko', 1150], ['Kurnia', 1090],['Lukman', 1020],
            ['Marno', 980], ['Nanda', 910], ['Ogi', 850],    ['Pras', 780],
        ];

        $players = collect();
        foreach ($playerData as [$name, $elo]) {
            $players->push(User::create([
                'discord_id'      => fake()->unique()->numerify('##################'),
                'username'        => $name,
                'discord_username'=> strtolower($name) . '#0000',
                'avatar'          => "https://ui-avatars.com/api/?name={$name}&background=" . substr(md5($name), 0, 6) . "&color=fff",
                'elo'             => $elo,
                'rank'            => EloService::division($elo),
                'wins'            => fake()->numberBetween(10, 80),
                'losses'          => fake()->numberBetween(5, 50),
                'draws'           => fake()->numberBetween(0, 10),
            ]));
        }

        // Create some completed matches
        $eloSvc  = new EloService();
        $shuffled = $players->shuffle()->take(10)->values();

        for ($i = 0; $i < 5; $i++) {
            $host  = $shuffled[$i * 2];
            $guest = $shuffled[$i * 2 + 1];
            $sh    = fake()->numberBetween(0, 5);
            $sg    = fake()->numberBetween(0, 5);

            $result = match (true) {
                $sh > $sg  => 'host',
                $sg > $sh  => 'guest',
                default    => 'draw',
            };

            $eloResult = $eloSvc->calculate($host->elo, $guest->elo, $result);

            GameMatch::create([
                'host_id'          => $host->id,
                'guest_id'         => $guest->id,
                'season_id'        => $season->id,
                'score_host'       => $sh,
                'score_guest'      => $sg,
                'winner_id'        => $result === 'draw' ? null : ($result === 'host' ? $host->id : $guest->id),
                'elo_change_host'  => $eloResult['host_change'],
                'elo_change_guest' => $eloResult['guest_change'],
                'status'           => 'completed',
                'created_at'       => now()->subDays(fake()->numberBetween(1, 30)),
                'updated_at'       => now()->subDays(fake()->numberBetween(0, 5)),
            ]);
        }

        $this->command->info('✅ PES League seeded!');
        $this->command->info('   Season 1 created');
        $this->command->info('   Players: ' . $players->count());
        $this->command->info('   Sample matches: 5');
        $this->command->newLine();
        $this->command->info('Next: php artisan vendor:publish --tag="permission-migrations"');
        $this->command->info('Then: php artisan migrate (to add permission tables)');
    }
}
