<?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_records', function (Blueprint $table) {
            $table->id();
            $table->uuid('public_id')->unique();
            $table->foreignId('player_profile_id')->constrained()->cascadeOnDelete();
            $table->foreignId('player_game_profile_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('game_id')->constrained()->cascadeOnDelete();
            $table->string('title');
            $table->text('description')->nullable();
            $table->string('record_type');
            $table->date('achievement_date')->nullable();
            $table->string('result_text')->nullable();
            $table->foreignId('rank_id')->nullable()->constrained('game_ranks')->nullOnDelete();
            $table->string('visibility')->default('public');
            $table->string('moderation_status')->default('pending');
            $table->text('moderation_reason')->nullable();
            $table->timestamp('published_at')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['player_profile_id', 'game_id', 'created_at']);
            $table->index('moderation_status');
        });

        Schema::create('player_record_media', function (Blueprint $table) {
            $table->id();
            $table->foreignId('player_record_id')->constrained()->cascadeOnDelete();
            $table->string('file_path');
            $table->string('thumbnail_path')->nullable();
            $table->string('original_name');
            $table->string('mime_type');
            $table->unsignedBigInteger('size');
            $table->string('checksum')->nullable();
            $table->string('caption')->nullable();
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();
        });

        Schema::create('player_videos', function (Blueprint $table) {
            $table->id();
            $table->uuid('public_id')->unique();
            $table->foreignId('player_profile_id')->constrained()->cascadeOnDelete();
            $table->foreignId('player_game_profile_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('game_id')->nullable()->constrained()->nullOnDelete();
            $table->string('title');
            $table->text('description')->nullable();
            $table->string('original_url');
            $table->string('provider');
            $table->string('external_video_id');
            $table->string('normalized_url');
            $table->string('safe_embed_url')->nullable();
            $table->string('thumbnail_url')->nullable();
            $table->string('video_type');
            $table->string('visibility')->default('public');
            $table->string('moderation_status')->default('pending');
            $table->text('moderation_reason')->nullable();
            $table->timestamp('published_at')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['player_profile_id', 'game_id', 'created_at']);
            $table->index('moderation_status');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('player_videos');
        Schema::dropIfExists('player_record_media');
        Schema::dropIfExists('player_records');
    }
};
