<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('topspender_claims', function (Blueprint $table) {
            $table->id();
            $table->unsignedInteger('user_id');
            $table->unsignedTinyInteger('month'); // 1-12
            $table->unsignedSmallInteger('year');
            $table->unsignedTinyInteger('rank'); // 1, 2, or 3
            $table->unsignedInteger('role_id'); // Character that received reward
            $table->string('role_name');
            $table->foreignId('reward_id')->constrained('topspender_rewards')->onDelete('cascade');
            $table->timestamp('claimed_at');
            $table->boolean('mail_sent')->default(false);
            $table->timestamps();

            // Unique constraint: one claim per user per month/year
            $table->unique(['user_id', 'month', 'year']);
            $table->index(['month', 'year']);
            $table->index('user_id');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('topspender_claims');
    }
};
