<?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('matches', function (Blueprint $table) {
            $table->id();
            $table->foreignId('host_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('guest_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('winner_id')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('season_id')->nullable()->constrained('seasons')->nullOnDelete();
            $table->unsignedTinyInteger('score_host')->nullable();
            $table->unsignedTinyInteger('score_guest')->nullable();
            // Submitted scores per player (for dispute detection)
            $table->unsignedTinyInteger('submitted_host_score_host')->nullable();
            $table->unsignedTinyInteger('submitted_host_score_guest')->nullable();
            $table->unsignedTinyInteger('submitted_guest_score_host')->nullable();
            $table->unsignedTinyInteger('submitted_guest_score_guest')->nullable();
            $table->integer('elo_change_host')->nullable();
            $table->integer('elo_change_guest')->nullable();
            $table->string('discord_channel_id')->nullable();
            $table->string('discord_voice_id')->nullable();
            // pending | in_progress | submitted | disputed | completed | cancelled
            $table->string('status')->default('pending');
            $table->text('admin_note')->nullable();
            $table->timestamps();

            $table->index(['host_id', 'created_at']);
            $table->index(['guest_id', 'created_at']);
            $table->index('status');
        });
    }

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