<?php

namespace Database\Seeders;

use App\Actions\Profile\CreatePlayerProfileOnRegistration;
use App\Enums\PlayerLevel;
use App\Enums\ProfileVisibility;
use App\Enums\UserRole;
use App\Enums\UserStatus;
use App\Models\PlayerLanguage;
use App\Models\PlayerPreference;
use App\Models\PlayerProfile;
use App\Models\PlayerSocialLink;
use App\Models\User;
use App\Services\ProfileCompletionService;
use App\Support\PublicId;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DemoUserSeeder extends Seeder
{
    public function run(): void
    {
        $createProfile = app(CreatePlayerProfileOnRegistration::class);
        $completion = app(ProfileCompletionService::class);

        foreach ($this->demoUsers() as $definition) {
            $user = User::updateOrCreate(
                ['email' => $definition['email']],
                [
                    'name' => $definition['display_name'],
                    'username' => $definition['username'],
                    'display_name' => $definition['display_name'],
                    'password' => Hash::make('password'),
                    'country_code' => $definition['country_code'],
                    'preferred_locale' => $definition['preferred_locale'] ?? 'en',
                    'timezone' => $definition['timezone'] ?? 'Africa/Cairo',
                    'phone_country_code' => $definition['phone_country_code'] ?? null,
                    'phone_number' => $definition['phone_number'] ?? null,
                    'phone_e164' => $definition['phone_e164'] ?? null,
                    'role' => UserRole::User,
                    'status' => UserStatus::Active,
                    'email_verified_at' => now(),
                    'terms_accepted_at' => now(),
                ],
            );

            if (blank($user->public_id)) {
                $user->update(['public_id' => PublicId::generate()]);
                $user->refresh();
            }

            if (! $user->playerProfile) {
                $createProfile->execute($user, [
                    'display_name' => $definition['display_name'],
                    'country_code' => $definition['country_code'],
                    'timezone' => $definition['timezone'] ?? 'Africa/Cairo',
                ]);
                $user->load('playerProfile');
            }

            $profile = $user->playerProfile;
            if (! $profile) {
                continue;
            }

            $profile->update([
                'display_name' => $definition['display_name'],
                'bio' => $definition['bio'],
                'country_code' => $definition['country_code'],
                'city' => $definition['city'] ?? null,
                'timezone' => $definition['timezone'] ?? 'Africa/Cairo',
                'birth_date' => $definition['birth_date'] ?? null,
                'player_level' => $definition['player_level'] ?? PlayerLevel::Amateur,
                'looking_for_team' => $definition['looking_for_team'] ?? false,
                'profile_visibility' => ProfileVisibility::Public,
            ]);

            foreach ($definition['social_links'] ?? [] as $index => $link) {
                PlayerSocialLink::updateOrCreate(
                    [
                        'player_profile_id' => $profile->id,
                        'platform' => $link['platform'],
                    ],
                    [
                        'url' => $link['url'],
                        'label' => $link['label'] ?? null,
                        'sort_order' => $index + 1,
                    ],
                );
            }

            foreach ($definition['languages'] ?? [] as $language) {
                PlayerLanguage::updateOrCreate(
                    [
                        'player_profile_id' => $profile->id,
                        'language_code' => $language['code'],
                    ],
                    ['proficiency' => $language['proficiency'] ?? 'fluent'],
                );
            }

            foreach ($definition['preferences'] ?? [] as $key => $value) {
                PlayerPreference::updateOrCreate(
                    [
                        'player_profile_id' => $profile->id,
                        'key' => $key,
                    ],
                    ['value' => $value],
                );
            }

            $profile->update([
                'profile_completion_percentage' => $completion->calculate($profile->fresh()),
            ]);
        }
    }

    private function demoUsers(): array
    {
        return [
            [
                'email' => 'nightfalcon@algaarena.gg',
                'username' => 'nightfalcon',
                'display_name' => 'NightFalcon',
                'country_code' => 'EG',
                'city' => 'Cairo',
                'timezone' => 'Africa/Cairo',
                'phone_country_code' => '20',
                'phone_number' => '1001234567',
                'phone_e164' => '+201001234567',
                'birth_date' => '1998-03-15',
                'player_level' => PlayerLevel::Professional,
                'looking_for_team' => false,
                'bio' => 'Professional tactical shooter player. MENA region competitor with multiple championship placements.',
                'social_links' => [
                    ['platform' => 'twitter', 'url' => 'https://twitter.com/nightfalcon', 'label' => 'Twitter'],
                    ['platform' => 'twitch', 'url' => 'https://twitch.tv/nightfalcon_gg', 'label' => 'Twitch'],
                ],
                'languages' => [
                    ['code' => 'ar', 'proficiency' => 'native'],
                    ['code' => 'en', 'proficiency' => 'fluent'],
                ],
                'preferences' => [
                    'notify_events' => true,
                    'notify_messages' => true,
                    'preferred_contact' => 'whatsapp',
                ],
            ],
            [
                'email' => 'stellarx@algaarena.gg',
                'username' => 'stellarx',
                'display_name' => 'StellarX',
                'country_code' => 'KR',
                'city' => 'Seoul',
                'timezone' => 'Asia/Seoul',
                'phone_country_code' => '82',
                'phone_number' => '1012345678',
                'phone_e164' => '+821012345678',
                'birth_date' => '1996-08-20',
                'player_level' => PlayerLevel::Professional,
                'looking_for_team' => false,
                'bio' => 'IGL with 5+ years competitive experience. Former world finalist building the next generation roster.',
                'social_links' => [
                    ['platform' => 'youtube', 'url' => 'https://youtube.com/@StellarX_Gaming', 'label' => 'YouTube'],
                ],
                'languages' => [
                    ['code' => 'ko', 'proficiency' => 'native'],
                    ['code' => 'en', 'proficiency' => 'fluent'],
                ],
                'preferences' => ['notify_events' => true],
            ],
            [
                'email' => 'ironclad@algaarena.gg',
                'username' => 'ironclad',
                'display_name' => 'IronClad',
                'country_code' => 'US',
                'city' => 'Los Angeles',
                'timezone' => 'America/Los_Angeles',
                'phone_country_code' => '1',
                'phone_number' => '5551234567',
                'phone_e164' => '+15551234567',
                'player_level' => PlayerLevel::SemiProfessional,
                'looking_for_team' => true,
                'bio' => 'Tank and support specialist across MOBA and battle royale titles. Open to structured team trials.',
                'social_links' => [
                    ['platform' => 'twitter', 'url' => 'https://twitter.com/ironclad_gg', 'label' => 'Twitter'],
                ],
                'languages' => [
                    ['code' => 'en', 'proficiency' => 'native'],
                ],
                'preferences' => ['looking_for_scrims' => true],
            ],
            [
                'email' => 'sandstorm@algaarena.gg',
                'username' => 'sandstorm',
                'display_name' => 'SandStorm',
                'country_code' => 'SA',
                'city' => 'Riyadh',
                'timezone' => 'Asia/Riyadh',
                'phone_country_code' => '966',
                'phone_number' => '501234567',
                'phone_e164' => '+966501234567',
                'player_level' => PlayerLevel::Amateur,
                'looking_for_team' => true,
                'bio' => 'Rising Valorant specialist from the Arabian Peninsula with fast reflexes and tournament ambition.',
                'languages' => [
                    ['code' => 'ar', 'proficiency' => 'native'],
                    ['code' => 'en', 'proficiency' => 'conversational'],
                ],
                'preferences' => ['notify_events' => true],
            ],
            [
                'email' => 'nova@algaarena.gg',
                'username' => 'novapulse',
                'display_name' => 'NovaPulse',
                'country_code' => 'AE',
                'city' => 'Dubai',
                'timezone' => 'Asia/Dubai',
                'phone_country_code' => '971',
                'phone_number' => '501234567',
                'phone_e164' => '+971501234567',
                'player_level' => PlayerLevel::SemiProfessional,
                'looking_for_team' => false,
                'bio' => 'CS2 rifler and Mobile Legends captain. Regular community event participant.',
                'social_links' => [
                    ['platform' => 'twitch', 'url' => 'https://twitch.tv/novapulse', 'label' => 'Twitch'],
                ],
                'languages' => [
                    ['code' => 'en', 'proficiency' => 'fluent'],
                    ['code' => 'ar', 'proficiency' => 'fluent'],
                ],
            ],
        ];
    }
}
