<?php

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

/**
 * A record of every leaderboard prize an admin has manually handed out, so the
 * same period isn't paid twice and the history is auditable.
 */
return new class extends Migration
{
    public function up()
    {
        Schema::create('leaderboard_awards', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->unsignedInteger('rank');
            $table->decimal('points', 20, 2)->default(0);
            $table->string('period_label')->nullable();
            $table->foreignId('awarded_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index(['period_label', 'rank']);
        });
    }

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