<?php

namespace Database\Seeders;

use App\Enums\ModerationStatus;
use App\Enums\ProfileVisibility;
use App\Enums\RecordType;
use App\Enums\VideoProvider;
use App\Enums\VideoType;
use App\Models\Game;
use App\Models\GameRank;
use App\Models\PlayerGameProfile;
use App\Models\PlayerRecord;
use App\Models\PlayerRecordMedia;
use App\Models\PlayerVideo;
use App\Models\User;
use Illuminate\Database\Seeder;

class PlayerContentSeeder extends Seeder
{
    public function run(): void
    {
        $this->seedRecords();
        $this->seedVideos();
    }

    private function seedRecords(): void
    {
        $nightfalcon = User::query()->where('email', 'nightfalcon@algaarena.gg')->with('playerProfile')->first();
        $valorant = Game::query()->where('slug', 'valorant')->first();

        if (! $nightfalcon?->playerProfile || ! $valorant) {
            return;
        }

        $gameProfile = PlayerGameProfile::query()
            ->where('player_profile_id', $nightfalcon->playerProfile->id)
            ->where('game_id', $valorant->id)
            ->first();

        $diamondRank = GameRank::query()
            ->where('game_id', $valorant->id)
            ->where('name', 'Diamond')
            ->first();

        $record = PlayerRecord::updateOrCreate(
            [
                'player_profile_id' => $nightfalcon->playerProfile->id,
                'title' => 'MENA Scrim Series — 13K ACS Match',
            ],
            [
                'player_game_profile_id' => $gameProfile?->id,
                'game_id' => $valorant->id,
                'description' => 'Personal best ACS in a ranked scrim series against semi-pro teams.',
                'record_type' => RecordType::MatchResult,
                'achievement_date' => now()->subWeeks(2)->toDateString(),
                'result_text' => '13-7 victory · 312 ACS',
                'rank_id' => $diamondRank?->id,
                'visibility' => ProfileVisibility::Public,
                'moderation_status' => ModerationStatus::Approved,
                'published_at' => now()->subWeeks(2),
            ],
        );

        PlayerRecordMedia::updateOrCreate(
            [
                'player_record_id' => $record->id,
                'sort_order' => 1,
            ],
            [
                'file_path' => 'records/demo/nightfalcon-scrim-scoreboard.webp',
                'thumbnail_path' => 'records/demo/nightfalcon-scrim-scoreboard.webp',
                'original_name' => 'scoreboard.webp',
                'mime_type' => 'image/webp',
                'size' => 102400,
                'checksum' => hash('sha256', 'demo-scoreboard-nightfalcon'),
                'caption' => 'Final scoreboard screenshot',
            ],
        );

        $stellarx = User::query()->where('email', 'stellarx@algaarena.gg')->with('playerProfile')->first();
        if ($stellarx?->playerProfile && $valorant) {
            PlayerRecord::updateOrCreate(
                [
                    'player_profile_id' => $stellarx->playerProfile->id,
                    'title' => 'Radiant Rank Achievement',
                ],
                [
                    'game_id' => $valorant->id,
                    'description' => 'Reached Radiant tier during Season open queue.',
                    'record_type' => RecordType::RankAchievement,
                    'achievement_date' => now()->subMonth()->toDateString(),
                    'result_text' => 'Radiant · Top 500',
                    'rank_id' => GameRank::query()->where('game_id', $valorant->id)->where('name', 'Radiant')->value('id'),
                    'visibility' => ProfileVisibility::Public,
                    'moderation_status' => ModerationStatus::Pending,
                ],
            );
        }
    }

    private function seedVideos(): void
    {
        $nightfalcon = User::query()->where('email', 'nightfalcon@algaarena.gg')->with('playerProfile')->first();
        $valorant = Game::query()->where('slug', 'valorant')->first();

        if (! $nightfalcon?->playerProfile || ! $valorant) {
            return;
        }

        $gameProfile = PlayerGameProfile::query()
            ->where('player_profile_id', $nightfalcon->playerProfile->id)
            ->where('game_id', $valorant->id)
            ->first();

        PlayerVideo::updateOrCreate(
            [
                'player_profile_id' => $nightfalcon->playerProfile->id,
                'external_video_id' => 'dQw4w9WgXcQ',
                'provider' => VideoProvider::YouTube,
            ],
            [
                'player_game_profile_id' => $gameProfile?->id,
                'game_id' => $valorant->id,
                'title' => 'Ace Clutch — Tournament Qualifier',
                'description' => '1v3 clutch to secure map point in ALGA qualifier.',
                'original_url' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
                'normalized_url' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
                'safe_embed_url' => 'https://www.youtube.com/embed/dQw4w9WgXcQ',
                'thumbnail_url' => 'https://img.youtube.com/vi/dQw4w9WgXcQ/hqdefault.jpg',
                'video_type' => VideoType::Highlight,
                'visibility' => ProfileVisibility::Public,
                'moderation_status' => ModerationStatus::Approved,
                'published_at' => now()->subDays(10),
            ],
        );

        $nova = User::query()->where('email', 'nova@algaarena.gg')->with('playerProfile')->first();
        $cs2 = Game::query()->where('slug', 'counter-strike-2')->first();

        if ($nova?->playerProfile && $cs2) {
            PlayerVideo::updateOrCreate(
                [
                    'player_profile_id' => $nova->playerProfile->id,
                    'external_video_id' => 'demo-cs2-match',
                    'provider' => VideoProvider::Twitch,
                ],
                [
                    'game_id' => $cs2->id,
                    'title' => 'Full Match VOD — Premier Queue',
                    'description' => 'Complete Premier match with comms.',
                    'original_url' => 'https://www.twitch.tv/videos/demo-cs2-match',
                    'normalized_url' => 'https://www.twitch.tv/videos/demo-cs2-match',
                    'safe_embed_url' => 'https://player.twitch.tv/?video=demo-cs2-match',
                    'video_type' => VideoType::FullMatch,
                    'visibility' => ProfileVisibility::Public,
                    'moderation_status' => ModerationStatus::Pending,
                ],
            );
        }
    }
}
