<?php

declare(strict_types=1);

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_reward_grants', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('world_id')->constrained('worlds')->cascadeOnDelete();
            $table->foreignId('artwork_id')->nullable()->constrained('artworks')->nullOnDelete();
            $table->foreignId('world_submission_id')->nullable()->constrained('world_submissions')->nullOnDelete();
            $table->foreignId('granted_by_user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->string('reward_type', 32);
            $table->string('grant_source', 16)->default('manual');
            $table->text('note')->nullable();
            $table->timestamp('granted_at')->nullable();
            $table->timestamps();

            $table->unique(['user_id', 'world_id', 'reward_type'], 'world_reward_grants_unique_reward');
            $table->index(['world_id', 'reward_type']);
            $table->index(['artwork_id']);
            $table->index(['world_submission_id']);
        });
    }

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