<?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('round_participants', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('round_id')->index();
            $table->unsignedBigInteger('user_id')->nullable()->index();
            $table->string('display_name', 120)->nullable();
            $table->string('avatar_id', 40)->nullable()->index();
            $table->unsignedSmallInteger('slot_number')->nullable();
            $table->boolean('is_bot')->default(false)->index();
            $table->boolean('is_alive')->default(true)->index();
            $table->unsignedTinyInteger('eliminated_stage')->nullable();
            $table->timestamp('eliminated_at')->nullable();
            $table->timestamp('joined_at')->nullable();
            $table->timestamps();

            $table->unique(['round_id', 'user_id']);
            $table->unique(['round_id', 'slot_number']);
        });

        Schema::table('rounds', function (Blueprint $table) {
            $table->unsignedTinyInteger('stage_current')->default(1)->after('prize_total');
            $table->unsignedTinyInteger('stage_total')->default(10)->after('stage_current');
            $table->timestamp('last_elimination_at')->nullable()->after('stage_total');
            $table->unsignedBigInteger('winner_user_id')->nullable()->after('last_elimination_at')->index();
            $table->string('winner_avatar_id', 40)->nullable()->after('winner_user_id');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('rounds', function (Blueprint $table) {
            $table->dropColumn([
                'stage_current',
                'stage_total',
                'last_elimination_at',
                'winner_user_id',
                'winner_avatar_id',
            ]);
        });

        Schema::dropIfExists('round_participants');
    }
};
