<?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::table('tournaments', function (Blueprint $table) {
            // BO config per phase: {"group":1,"knockout":2,"final":2}
            // Values: 1=BO1, 2=BO2, 3=BO3, etc.
            $table->json('bo_config')->nullable()->after('groups_per_advance');

            // Ban config per phase: {"group":3,"knockout":3,"final":3}
            // Values: number of clubs each player can ban
            $table->json('ban_config')->nullable()->after('bo_config');
        });

        Schema::table('tournament_matches', function (Blueprint $table) {
            // Which leg this match belongs to (1, 2, 3...) for BO2+
            $table->unsignedTinyInteger('leg_number')->default(1)->after('match_number');

            // Links legs of the same encounter together (leg 2,3 point back to leg 1)
            $table->foreignId('series_parent_id')->nullable()->after('leg_number')
                  ->constrained('tournament_matches')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('tournament_matches', function (Blueprint $table) {
            $table->dropConstrainedForeignId('series_parent_id');
            $table->dropColumn(['leg_number']);
        });
        Schema::table('tournaments', function (Blueprint $table) {
            $table->dropColumn(['bo_config', 'ban_config']);
        });
    }
};
