<?php

namespace Database\Seeders;

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\PlayerGameCharacter;
use App\Models\PlayerGamePlatform;
use App\Models\PlayerGameProfile;
use App\Models\PlayerGameRole;
use App\Models\User;
use Illuminate\Database\Seeder;

class PlayerGameProfileSeeder extends Seeder
{
    public function run(): void
    {
        $definitions = [
            'nightfalcon@algaarena.gg' => [
                [
                    'game_slug' => 'valorant',
                    'in_game_name' => 'NightFalcon#EG1',
                    'current_rank' => 'Diamond',
                    'highest_rank' => 'Immortal',
                    'platform' => 'pc',
                    'region' => 'MENA',
                    'roles' => ['Duelist'],
                    'primary_role' => 'Duelist',
                    'characters' => ['Jett', 'Reyna'],
                    'is_primary' => true,
                    'years' => 4,
                ],
                [
                    'game_slug' => 'league-of-legends',
                    'in_game_name' => 'NightFalcon',
                    'current_rank' => 'Platinum',
                    'highest_rank' => 'Diamond',
                    'platform' => 'pc',
                    'region' => 'EUW',
                    'roles' => ['Mid'],
                    'primary_role' => 'Mid',
                    'characters' => ['Ahri'],
                    'years' => 3,
                ],
            ],
            'stellarx@algaarena.gg' => [
                [
                    'game_slug' => 'valorant',
                    'in_game_name' => 'StellarX#KR1',
                    'current_rank' => 'Radiant',
                    'highest_rank' => 'Radiant',
                    'platform' => 'pc',
                    'region' => 'KR',
                    'roles' => ['IGL', 'Initiator'],
                    'primary_role' => 'IGL',
                    'characters' => ['Omen', 'Sage'],
                    'is_primary' => true,
                    'years' => 6,
                ],
            ],
            'ironclad@algaarena.gg' => [
                [
                    'game_slug' => 'league-of-legends',
                    'in_game_name' => 'IronClad',
                    'current_rank' => 'Grandmaster',
                    'highest_rank' => 'Grandmaster',
                    'platform' => 'pc',
                    'region' => 'NA',
                    'roles' => ['Support', 'Top'],
                    'primary_role' => 'Support',
                    'characters' => ['Thresh', 'Lux'],
                    'is_primary' => true,
                    'years' => 7,
                ],
                [
                    'game_slug' => 'pubg-mobile',
                    'in_game_name' => 'IronCladPUBG',
                    'current_rank' => 'Ace',
                    'highest_rank' => 'Conqueror',
                    'platform' => 'android',
                    'region' => 'ME',
                    'roles' => ['Support'],
                    'primary_role' => 'Support',
                    'years' => 2,
                ],
            ],
            'sandstorm@algaarena.gg' => [
                [
                    'game_slug' => 'valorant',
                    'in_game_name' => 'SandStorm#SA1',
                    'current_rank' => 'Platinum',
                    'highest_rank' => 'Diamond',
                    'platform' => 'pc',
                    'region' => 'MENA',
                    'roles' => ['Duelist'],
                    'primary_role' => 'Duelist',
                    'characters' => ['Reyna'],
                    'is_primary' => true,
                    'years' => 2,
                ],
            ],
            'nova@algaarena.gg' => [
                [
                    'game_slug' => 'counter-strike-2',
                    'in_game_name' => 'NovaPulse',
                    'current_rank' => 'Premier 10k',
                    'highest_rank' => 'Premier 20k',
                    'platform' => 'pc',
                    'region' => 'ME',
                    'roles' => ['Entry Fragger', 'IGL'],
                    'primary_role' => 'Entry Fragger',
                    'is_primary' => true,
                    'years' => 5,
                ],
                [
                    'game_slug' => 'mobile-legends',
                    'in_game_name' => 'NovaPulseML',
                    'current_rank' => 'Mythic',
                    'highest_rank' => 'Mythical Glory',
                    'platform' => 'android',
                    'region' => 'ME',
                    'roles' => ['Mid Lane', 'IGL'],
                    'primary_role' => 'Mid Lane',
                    'characters' => ['Gusion', 'Kagura'],
                    'years' => 4,
                ],
            ],
        ];

        foreach ($definitions as $email => $profiles) {
            $user = User::query()->where('email', $email)->with('playerProfile')->first();
            if (! $user?->playerProfile) {
                continue;
            }

            foreach ($profiles as $definition) {
                $this->seedGameProfile($user->playerProfile->id, $definition);
            }
        }
    }

    private function seedGameProfile(int $playerProfileId, array $definition): void
    {
        $game = Game::query()->where('slug', $definition['game_slug'])->first();
        if (! $game) {
            return;
        }

        $currentRank = GameRank::query()
            ->where('game_id', $game->id)
            ->where('name', $definition['current_rank'])
            ->first();
        $highestRank = GameRank::query()
            ->where('game_id', $game->id)
            ->where('name', $definition['highest_rank'])
            ->first();
        $platform = GamePlatform::query()
            ->where('game_id', $game->id)
            ->where('slug', $definition['platform'])
            ->first();
        $region = GameRegion::query()
            ->where('game_id', $game->id)
            ->where('code', $definition['region'])
            ->first();

        $gameProfile = PlayerGameProfile::updateOrCreate(
            [
                'player_profile_id' => $playerProfileId,
                'game_id' => $game->id,
            ],
            [
                'in_game_name' => $definition['in_game_name'],
                'current_rank_id' => $currentRank?->id,
                'highest_rank_id' => $highestRank?->id,
                'primary_platform_id' => $platform?->id,
                'region_id' => $region?->id,
                'years_of_experience' => $definition['years'] ?? null,
                'skill_level' => $definition['skill_level'] ?? 'competitive',
                'biography' => $definition['biography'] ?? null,
                'is_primary_game' => $definition['is_primary'] ?? false,
                'is_public' => true,
            ],
        );

        if ($platform) {
            PlayerGamePlatform::updateOrCreate(
                [
                    'player_game_profile_id' => $gameProfile->id,
                    'game_platform_id' => $platform->id,
                ],
            );
        }

        foreach ($definition['roles'] ?? [] as $roleName) {
            $role = GameRole::query()
                ->where('game_id', $game->id)
                ->where('name', $roleName)
                ->first();

            if (! $role) {
                continue;
            }

            PlayerGameRole::updateOrCreate(
                [
                    'player_game_profile_id' => $gameProfile->id,
                    'game_role_id' => $role->id,
                ],
                [
                    'is_primary' => ($definition['primary_role'] ?? null) === $roleName,
                ],
            );
        }

        foreach ($definition['characters'] ?? [] as $index => $characterName) {
            $character = GameCharacter::query()
                ->where('game_id', $game->id)
                ->where('name', $characterName)
                ->first();

            if (! $character) {
                continue;
            }

            PlayerGameCharacter::updateOrCreate(
                [
                    'player_game_profile_id' => $gameProfile->id,
                    'game_character_id' => $character->id,
                ],
                ['sort_order' => $index + 1],
            );
        }
    }
}
