<?php

namespace Database\Seeders;

use App\Enums\GameStatus;
use App\Enums\GameType;
use App\Models\Game;
use App\Models\GameCharacter;
use App\Models\GamePlatform;
use App\Models\GameRank;
use App\Models\GameRegion;
use App\Models\GameRole;
use App\Models\GameTranslation;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class GameSeeder extends Seeder
{
    public function run(): void
    {
        $games = $this->games();

        foreach ($games as $index => $definition) {
            $game = Game::updateOrCreate(
                ['slug' => $definition['slug']],
                [
                    'name' => $definition['name'],
                    'short_name' => $definition['short_name'] ?? null,
                    'publisher' => $definition['publisher'] ?? null,
                    'description' => $definition['description'] ?? null,
                    'logo_path' => $definition['logo_path'],
                    'cover_path' => $definition['cover_path'],
                    'game_type' => $definition['game_type'],
                    'status' => GameStatus::Active,
                    'sort_order' => $index + 1,
                    'supports_ranks' => $definition['supports_ranks'] ?? true,
                    'supports_roles' => $definition['supports_roles'] ?? true,
                    'supports_characters' => $definition['supports_characters'] ?? false,
                    'supports_regions' => $definition['supports_regions'] ?? true,
                    'supports_servers' => $definition['supports_servers'] ?? false,
                ],
            );

            foreach ($definition['translations'] as $locale => $translation) {
                GameTranslation::updateOrCreate(
                    ['game_id' => $game->id, 'locale' => $locale],
                    [
                        'name' => $translation['name'],
                        'description' => $translation['description'] ?? null,
                    ],
                );
            }

            $this->seedPlatforms($game, $definition['platforms']);
            $this->seedRegions($game, $definition['regions']);
            $this->seedRanks($game, $definition['ranks']);
            $this->seedRoles($game, $definition['roles'] ?? []);
            $this->seedCharacters($game, $definition['characters'] ?? []);
        }
    }

    private function seedPlatforms(Game $game, array $platforms): void
    {
        foreach ($platforms as $index => $platform) {
            GamePlatform::updateOrCreate(
                ['game_id' => $game->id, 'slug' => $platform['slug']],
                [
                    'name' => $platform['name'],
                    'sort_order' => $index + 1,
                ],
            );
        }
    }

    private function seedRegions(Game $game, array $regions): void
    {
        foreach ($regions as $index => $region) {
            GameRegion::updateOrCreate(
                ['game_id' => $game->id, 'code' => $region['code']],
                [
                    'name' => $region['name'],
                    'sort_order' => $index + 1,
                ],
            );
        }
    }

    private function seedRanks(Game $game, array $ranks): void
    {
        foreach ($ranks as $index => $rank) {
            GameRank::updateOrCreate(
                ['game_id' => $game->id, 'name' => $rank['name']],
                [
                    'color' => $rank['color'] ?? null,
                    'sort_order' => $index + 1,
                    'min_value' => $rank['min_value'] ?? null,
                ],
            );
        }
    }

    private function seedRoles(Game $game, array $roles): void
    {
        foreach ($roles as $index => $role) {
            GameRole::updateOrCreate(
                ['game_id' => $game->id, 'name' => $role],
                ['sort_order' => $index + 1],
            );
        }
    }

    private function seedCharacters(Game $game, array $characters): void
    {
        foreach ($characters as $index => $character) {
            GameCharacter::updateOrCreate(
                ['game_id' => $game->id, 'name' => $character],
                ['sort_order' => $index + 1],
            );
        }
    }

    private function games(): array
    {
        return [
            $this->valorant(),
            $this->leagueOfLegends(),
            $this->eaSportsFc(),
            $this->tekken(),
            $this->pubgMobile(),
            $this->counterStrike2(),
            $this->mobileLegends(),
        ];
    }

    private function valorant(): array
    {
        return [
            'slug' => 'valorant',
            'name' => 'VALORANT',
            'short_name' => 'VAL',
            'publisher' => 'Riot Games',
            'description' => 'Tactical 5v5 character-based shooter.',
            'logo_path' => '/assets/images/generated/games/game-valorstrike.webp',
            'cover_path' => '/assets/images/generated/games/game-valorstrike.webp',
            'game_type' => GameType::Team,
            'supports_characters' => true,
            'supports_servers' => true,
            'translations' => [
                'en' => ['name' => 'VALORANT', 'description' => 'Tactical 5v5 character-based shooter.'],
                'ar' => ['name' => 'فالورانت', 'description' => 'لعبة إطلاق نار تكتيكية 5 ضد 5 قائمة على الشخصيات.'],
            ],
            'platforms' => [
                ['name' => 'PC', 'slug' => 'pc'],
            ],
            'regions' => [
                ['name' => 'Europe', 'code' => 'EU'],
                ['name' => 'North America', 'code' => 'NA'],
                ['name' => 'Middle East & North Africa', 'code' => 'MENA'],
                ['name' => 'Asia Pacific', 'code' => 'APAC'],
                ['name' => 'Korea', 'code' => 'KR'],
            ],
            'ranks' => [
                ['name' => 'Iron', 'color' => '#8B7355', 'min_value' => 0],
                ['name' => 'Bronze', 'color' => '#CD7F32', 'min_value' => 100],
                ['name' => 'Silver', 'color' => '#A8B4C0', 'min_value' => 300],
                ['name' => 'Gold', 'color' => '#FFD700', 'min_value' => 500],
                ['name' => 'Platinum', 'color' => '#E5E4E2', 'min_value' => 800],
                ['name' => 'Diamond', 'color' => '#B9F2FF', 'min_value' => 1200],
                ['name' => 'Ascendant', 'color' => '#1DB954', 'min_value' => 1600],
                ['name' => 'Immortal', 'color' => '#FF3B57', 'min_value' => 2000],
                ['name' => 'Radiant', 'color' => '#FFE566', 'min_value' => 2500],
            ],
            'roles' => ['Duelist', 'Controller', 'Initiator', 'Sentinel', 'IGL'],
            'characters' => ['Jett', 'Sage', 'Omen', 'Reyna', 'Killjoy'],
        ];
    }

    private function leagueOfLegends(): array
    {
        return [
            'slug' => 'league-of-legends',
            'name' => 'League of Legends',
            'short_name' => 'LoL',
            'publisher' => 'Riot Games',
            'description' => 'Team-based MOBA with ranked solo and flex queues.',
            'logo_path' => '/assets/images/generated/games/game-frontline.webp',
            'cover_path' => '/assets/images/generated/games/game-frontline.webp',
            'game_type' => GameType::Team,
            'supports_characters' => true,
            'translations' => [
                'en' => ['name' => 'League of Legends', 'description' => 'Team-based MOBA with ranked solo and flex queues.'],
                'ar' => ['name' => 'ليج أوف ليجندز', 'description' => 'لعبة MOBA جماعية مع تصنيف فردي ومرن.'],
            ],
            'platforms' => [
                ['name' => 'PC', 'slug' => 'pc'],
            ],
            'regions' => [
                ['name' => 'Europe West', 'code' => 'EUW'],
                ['name' => 'Europe Nordic & East', 'code' => 'EUNE'],
                ['name' => 'North America', 'code' => 'NA'],
                ['name' => 'Middle East', 'code' => 'ME'],
                ['name' => 'Korea', 'code' => 'KR'],
            ],
            'ranks' => [
                ['name' => 'Iron', 'color' => '#8B7355', 'min_value' => 0],
                ['name' => 'Bronze', 'color' => '#CD7F32', 'min_value' => 400],
                ['name' => 'Silver', 'color' => '#A8B4C0', 'min_value' => 800],
                ['name' => 'Gold', 'color' => '#FFD700', 'min_value' => 1200],
                ['name' => 'Platinum', 'color' => '#E5E4E2', 'min_value' => 1600],
                ['name' => 'Emerald', 'color' => '#1DB954', 'min_value' => 2000],
                ['name' => 'Diamond', 'color' => '#B9F2FF', 'min_value' => 2400],
                ['name' => 'Master', 'color' => '#7C5CFC', 'min_value' => 2800],
                ['name' => 'Grandmaster', 'color' => '#FF3B57', 'min_value' => 3200],
                ['name' => 'Challenger', 'color' => '#FFE566', 'min_value' => 3600],
            ],
            'roles' => ['Top', 'Jungle', 'Mid', 'ADC', 'Support', 'IGL'],
            'characters' => ['Ahri', 'Yasuo', 'Lux', 'Lee Sin', 'Thresh'],
        ];
    }

    private function eaSportsFc(): array
    {
        return [
            'slug' => 'ea-sports-fc',
            'name' => 'EA SPORTS FC',
            'short_name' => 'FC',
            'publisher' => 'Electronic Arts',
            'description' => 'Competitive football with Ultimate Team and Pro Clubs.',
            'logo_path' => '/assets/images/generated/games/game-nexus-ops.webp',
            'cover_path' => '/assets/images/generated/games/game-nexus-ops.webp',
            'game_type' => GameType::Both,
            'supports_roles' => true,
            'supports_characters' => false,
            'translations' => [
                'en' => ['name' => 'EA SPORTS FC', 'description' => 'Competitive football with Ultimate Team and Pro Clubs.'],
                'ar' => ['name' => 'إي إيه سبورتس إف سي', 'description' => 'كرة قدم تنافسية مع Ultimate Team و Pro Clubs.'],
            ],
            'platforms' => [
                ['name' => 'PlayStation', 'slug' => 'playstation'],
                ['name' => 'Xbox', 'slug' => 'xbox'],
                ['name' => 'PC', 'slug' => 'pc'],
            ],
            'regions' => [
                ['name' => 'Europe', 'code' => 'EU'],
                ['name' => 'Middle East', 'code' => 'ME'],
                ['name' => 'North America', 'code' => 'NA'],
                ['name' => 'Asia', 'code' => 'ASIA'],
            ],
            'ranks' => [
                ['name' => 'Division 10', 'color' => '#8B7355', 'min_value' => 0],
                ['name' => 'Division 5', 'color' => '#A8B4C0', 'min_value' => 500],
                ['name' => 'Division 1', 'color' => '#FFD700', 'min_value' => 1000],
                ['name' => 'Elite', 'color' => '#B9F2FF', 'min_value' => 1500],
                ['name' => 'Champions', 'color' => '#FF3B57', 'min_value' => 2000],
            ],
            'roles' => ['Striker', 'Winger', 'Midfielder', 'Defender', 'Goalkeeper', 'Captain'],
        ];
    }

    private function tekken(): array
    {
        return [
            'slug' => 'tekken',
            'name' => 'Tekken 8',
            'short_name' => 'TK8',
            'publisher' => 'Bandai Namco',
            'description' => '1v1 fighting game with global esports circuits.',
            'logo_path' => '/assets/images/generated/games/game-strikezone.webp',
            'cover_path' => '/assets/images/generated/games/game-strikezone.webp',
            'game_type' => GameType::Solo,
            'supports_roles' => false,
            'supports_characters' => true,
            'supports_regions' => true,
            'translations' => [
                'en' => ['name' => 'Tekken 8', 'description' => '1v1 fighting game with global esports circuits.'],
                'ar' => ['name' => 'تيكن 8', 'description' => 'لعبة قتال فردية مع بطولات إلكترونية عالمية.'],
            ],
            'platforms' => [
                ['name' => 'PlayStation', 'slug' => 'playstation'],
                ['name' => 'Xbox', 'slug' => 'xbox'],
                ['name' => 'PC', 'slug' => 'pc'],
            ],
            'regions' => [
                ['name' => 'Global', 'code' => 'GLOBAL'],
                ['name' => 'Middle East', 'code' => 'ME'],
                ['name' => 'Europe', 'code' => 'EU'],
                ['name' => 'North America', 'code' => 'NA'],
                ['name' => 'Asia', 'code' => 'ASIA'],
            ],
            'ranks' => [
                ['name' => 'Beginner', 'color' => '#8B7355', 'min_value' => 0],
                ['name' => 'Intermediate', 'color' => '#A8B4C0', 'min_value' => 1000],
                ['name' => 'Advanced', 'color' => '#FFD700', 'min_value' => 2000],
                ['name' => 'Expert', 'color' => '#B9F2FF', 'min_value' => 3000],
                ['name' => 'Tekken King', 'color' => '#FF3B57', 'min_value' => 4000],
                ['name' => 'Tekken God', 'color' => '#7C5CFC', 'min_value' => 5000],
            ],
            'characters' => ['Jin Kazama', 'Paul Phoenix', 'King', 'Lili', 'Hwoarang'],
        ];
    }

    private function pubgMobile(): array
    {
        return [
            'slug' => 'pubg-mobile',
            'name' => 'PUBG Mobile',
            'short_name' => 'PUBGM',
            'publisher' => 'Tencent / Krafton',
            'description' => 'Mobile battle royale with squad-based competitive play.',
            'logo_path' => '/assets/images/generated/games/game-nexus-ops.webp',
            'cover_path' => '/assets/images/generated/games/game-nexus-ops.webp',
            'game_type' => GameType::Team,
            'supports_servers' => true,
            'translations' => [
                'en' => ['name' => 'PUBG Mobile', 'description' => 'Mobile battle royale with squad-based competitive play.'],
                'ar' => ['name' => 'ببجي موبايل', 'description' => 'باتل رويال على الجوال مع لعب تنافسي جماعي.'],
            ],
            'platforms' => [
                ['name' => 'Android', 'slug' => 'android'],
                ['name' => 'iOS', 'slug' => 'ios'],
            ],
            'regions' => [
                ['name' => 'Middle East', 'code' => 'ME'],
                ['name' => 'Europe', 'code' => 'EU'],
                ['name' => 'Asia', 'code' => 'ASIA'],
                ['name' => 'North America', 'code' => 'NA'],
            ],
            'ranks' => [
                ['name' => 'Bronze', 'color' => '#CD7F32', 'min_value' => 0],
                ['name' => 'Silver', 'color' => '#A8B4C0', 'min_value' => 1500],
                ['name' => 'Gold', 'color' => '#FFD700', 'min_value' => 2200],
                ['name' => 'Platinum', 'color' => '#E5E4E2', 'min_value' => 2700],
                ['name' => 'Diamond', 'color' => '#B9F2FF', 'min_value' => 3200],
                ['name' => 'Crown', 'color' => '#FF3B57', 'min_value' => 3600],
                ['name' => 'Ace', 'color' => '#7C5CFC', 'min_value' => 4000],
                ['name' => 'Conqueror', 'color' => '#FFE566', 'min_value' => 4500],
            ],
            'roles' => ['IGL', 'Fragger', 'Support', 'Sniper', 'Scout'],
        ];
    }

    private function counterStrike2(): array
    {
        return [
            'slug' => 'counter-strike-2',
            'name' => 'Counter-Strike 2',
            'short_name' => 'CS2',
            'publisher' => 'Valve',
            'description' => 'Tactical FPS with Premier and community competitive modes.',
            'logo_path' => '/assets/images/generated/games/game-strikezone.webp',
            'cover_path' => '/assets/images/generated/games/game-strikezone.webp',
            'game_type' => GameType::Team,
            'translations' => [
                'en' => ['name' => 'Counter-Strike 2', 'description' => 'Tactical FPS with Premier and community competitive modes.'],
                'ar' => ['name' => 'كاونتر سترايك 2', 'description' => 'إطلاق نار تكتيكي مع أوضاع Premier والتنافس المجتمعي.'],
            ],
            'platforms' => [
                ['name' => 'PC', 'slug' => 'pc'],
            ],
            'regions' => [
                ['name' => 'Europe', 'code' => 'EU'],
                ['name' => 'North America', 'code' => 'NA'],
                ['name' => 'Middle East', 'code' => 'ME'],
                ['name' => 'Asia', 'code' => 'ASIA'],
                ['name' => 'Oceania', 'code' => 'OCE'],
            ],
            'ranks' => [
                ['name' => 'Silver I', 'color' => '#A8B4C0', 'min_value' => 0],
                ['name' => 'Gold Nova', 'color' => '#FFD700', 'min_value' => 5000],
                ['name' => 'Master Guardian', 'color' => '#E5E4E2', 'min_value' => 10000],
                ['name' => 'Legendary Eagle', 'color' => '#B9F2FF', 'min_value' => 15000],
                ['name' => 'Supreme', 'color' => '#FF3B57', 'min_value' => 20000],
                ['name' => 'Global Elite', 'color' => '#FFE566', 'min_value' => 25000],
                ['name' => 'Premier 10k', 'color' => '#7C5CFC', 'min_value' => 10000],
                ['name' => 'Premier 20k', 'color' => '#1DB954', 'min_value' => 20000],
            ],
            'roles' => ['AWPer', 'Entry Fragger', 'Lurker', 'Support', 'IGL', 'Anchor'],
        ];
    }

    private function mobileLegends(): array
    {
        return [
            'slug' => 'mobile-legends',
            'name' => 'Mobile Legends: Bang Bang',
            'short_name' => 'MLBB',
            'publisher' => 'Moonton',
            'description' => 'Mobile MOBA with ranked and professional league play.',
            'logo_path' => '/assets/images/generated/games/game-frontline.webp',
            'cover_path' => '/assets/images/generated/games/game-frontline.webp',
            'game_type' => GameType::Team,
            'supports_characters' => true,
            'translations' => [
                'en' => ['name' => 'Mobile Legends: Bang Bang', 'description' => 'Mobile MOBA with ranked and professional league play.'],
                'ar' => ['name' => 'موبايل ليجندز: بانغ بانغ', 'description' => 'MOBA على الجوال مع تصنيف وبطولات محترفة.'],
            ],
            'platforms' => [
                ['name' => 'Android', 'slug' => 'android'],
                ['name' => 'iOS', 'slug' => 'ios'],
            ],
            'regions' => [
                ['name' => 'Middle East', 'code' => 'ME'],
                ['name' => 'Southeast Asia', 'code' => 'SEA'],
                ['name' => 'Europe', 'code' => 'EU'],
                ['name' => 'North America', 'code' => 'NA'],
            ],
            'ranks' => [
                ['name' => 'Warrior', 'color' => '#8B7355', 'min_value' => 0],
                ['name' => 'Elite', 'color' => '#A8B4C0', 'min_value' => 500],
                ['name' => 'Master', 'color' => '#FFD700', 'min_value' => 1000],
                ['name' => 'Grandmaster', 'color' => '#E5E4E2', 'min_value' => 1500],
                ['name' => 'Epic', 'color' => '#B9F2FF', 'min_value' => 2000],
                ['name' => 'Legend', 'color' => '#FF3B57', 'min_value' => 2500],
                ['name' => 'Mythic', 'color' => '#7C5CFC', 'min_value' => 3000],
                ['name' => 'Mythical Glory', 'color' => '#FFE566', 'min_value' => 3500],
            ],
            'roles' => ['Gold Lane', 'Exp Lane', 'Mid Lane', 'Roamer', 'Jungler', 'IGL'],
            'characters' => ['Layla', 'Gusion', 'Franco', 'Kagura', 'Lancelot'],
        ];
    }
}
