<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('player_profiles', function (Blueprint $table) {
            $table->id();
            $table->uuid('public_id')->unique();
            $table->foreignId('user_id')->unique()->constrained()->cascadeOnDelete();
            $table->string('display_name');
            $table->text('bio')->nullable();
            $table->string('avatar_path')->nullable();
            $table->string('cover_path')->nullable();
            $table->string('country_code', 2)->nullable();
            $table->string('city')->nullable();
            $table->string('timezone')->default('UTC');
            $table->date('birth_date')->nullable();
            $table->string('player_level')->default('amateur');
            $table->boolean('looking_for_team')->default(false);
            $table->string('profile_visibility')->default('public');
            $table->unsignedTinyInteger('profile_completion_percentage')->default(0);
            $table->string('status')->default('active');
            $table->timestamps();
            $table->softDeletes();

            $table->index(['country_code', 'looking_for_team']);
        });

        Schema::create('player_social_links', function (Blueprint $table) {
            $table->id();
            $table->foreignId('player_profile_id')->constrained()->cascadeOnDelete();
            $table->string('platform');
            $table->string('url');
            $table->string('label')->nullable();
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();
        });

        Schema::create('player_languages', function (Blueprint $table) {
            $table->id();
            $table->foreignId('player_profile_id')->constrained()->cascadeOnDelete();
            $table->string('language_code', 5);
            $table->string('proficiency')->nullable();
            $table->timestamps();

            $table->unique(['player_profile_id', 'language_code']);
        });

        Schema::create('player_preferences', function (Blueprint $table) {
            $table->id();
            $table->foreignId('player_profile_id')->constrained()->cascadeOnDelete();
            $table->string('key');
            $table->json('value')->nullable();
            $table->timestamps();

            $table->unique(['player_profile_id', 'key']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('player_preferences');
        Schema::dropIfExists('player_languages');
        Schema::dropIfExists('player_social_links');
        Schema::dropIfExists('player_profiles');
    }
};
