<?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_game_profiles', function (Blueprint $table) {
            $table->id();
            $table->uuid('public_id')->unique();
            $table->foreignId('player_profile_id')->constrained()->cascadeOnDelete();
            $table->foreignId('game_id')->constrained()->cascadeOnDelete();
            $table->string('in_game_name');
            $table->string('external_game_id')->nullable();
            $table->foreignId('current_rank_id')->nullable()->constrained('game_ranks')->nullOnDelete();
            $table->foreignId('highest_rank_id')->nullable()->constrained('game_ranks')->nullOnDelete();
            $table->foreignId('primary_platform_id')->nullable()->constrained('game_platforms')->nullOnDelete();
            $table->foreignId('region_id')->nullable()->constrained('game_regions')->nullOnDelete();
            $table->foreignId('server_id')->nullable()->constrained('game_servers')->nullOnDelete();
            $table->unsignedTinyInteger('years_of_experience')->nullable();
            $table->string('skill_level')->nullable();
            $table->text('biography')->nullable();
            $table->boolean('is_primary_game')->default(false);
            $table->boolean('is_public')->default(true);
            $table->timestamps();
            $table->softDeletes();

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

        Schema::create('player_game_roles', function (Blueprint $table) {
            $table->id();
            $table->foreignId('player_game_profile_id')->constrained()->cascadeOnDelete();
            $table->foreignId('game_role_id')->constrained()->cascadeOnDelete();
            $table->boolean('is_primary')->default(false);
            $table->timestamps();

            $table->unique(['player_game_profile_id', 'game_role_id']);
        });

        Schema::create('player_game_characters', function (Blueprint $table) {
            $table->id();
            $table->foreignId('player_game_profile_id')->constrained()->cascadeOnDelete();
            $table->foreignId('game_character_id')->constrained()->cascadeOnDelete();
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();

            $table->unique(['player_game_profile_id', 'game_character_id'], 'pgc_profile_character_unique');
        });

        Schema::create('player_game_platforms', function (Blueprint $table) {
            $table->id();
            $table->foreignId('player_game_profile_id')->constrained()->cascadeOnDelete();
            $table->foreignId('game_platform_id')->constrained()->cascadeOnDelete();
            $table->timestamps();

            $table->unique(['player_game_profile_id', 'game_platform_id'], 'pgp_platform_unique');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('player_game_platforms');
        Schema::dropIfExists('player_game_characters');
        Schema::dropIfExists('player_game_roles');
        Schema::dropIfExists('player_game_profiles');
    }
};
