<?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('world_submissions', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('world_id')->constrained('worlds')->cascadeOnDelete();
            $table->foreignId('artwork_id')->constrained('artworks')->cascadeOnDelete();
            $table->foreignId('submitted_by_user_id')->constrained('users')->cascadeOnDelete();
            $table->string('status', 24)->default('pending');
            $table->text('note')->nullable();
            $table->text('reviewer_note')->nullable();
            $table->foreignId('reviewed_by_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('reviewed_at')->nullable();
            $table->timestamps();

            $table->unique(['world_id', 'artwork_id'], 'world_submissions_world_artwork_unique');
            $table->index(['world_id', 'status', 'reviewed_at'], 'world_submissions_world_status_idx');
            $table->index(['artwork_id', 'status'], 'world_submissions_artwork_status_idx');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('world_submissions');
    }
};