<?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
    {
        // Create competitions table
        Schema::create('competitions', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->text('description')->nullable();
            $table->date('start_date');
            $table->string('status')->default('planned');
            $table->string('location')->nullable();
            $table->integer('max_teams')->default(0);
            $table->integer('current_scheduling_mode_id')->nullable();
            $table->uuid('owner')->nullable();
            $table->timestamps();
        });

        // Create scheduling modes table
        Schema::create('scheduling_modes', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->text('description')->nullable();
            $table->integer('competition_id')->nullable();
            $table->string('algorithm')->default('round_robin');
            $table->json('config')->nullable();
            $table->integer('sequence_order')->default(1);
            $table->string('status')->default('active');
            $table->timestamps();
        });

        // Create fields table
        Schema::create('fields', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('location')->nullable();
            $table->text('description')->nullable();
            $table->string('status')->default('planned');
            $table->integer('capacity')->default(0);
            $table->string('surface_type')->default('grass');
            $table->boolean('indoor')->default(false);
            $table->json('dimensions')->nullable();
            $table->timestamps();
        });

        Schema::create('competition_field', function (Blueprint $table) {
            $table->id();
            $table->foreignId('competition_id')->constrained('competitions')->onDelete('cascade');
            $table->foreignId('field_id')->constrained('fields')->onDelete('cascade');
            $table->timestamps();
        });

        // Create teams table
        Schema::create('teams', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('country')->nullable();
            $table->string('city')->nullable();
            $table->string('address')->nullable();
            $table->string('phone')->nullable();
            $table->timestamps();
        });

        Schema::create('competition_team', function (Blueprint $table) {
            $table->id();
            $table->foreignId('competition_id')->constrained('competitions')->onDelete('cascade');
            $table->foreignId('team_id')->constrained('teams')->onDelete('cascade');
            $table->string('status')->default('confirmed');
            $table->timestamps();
        });

        // Create matches table
        Schema::create('matches', function (Blueprint $table) {
            $table->id();
            $table->foreignId('competition_id')->constrained('competitions')->onDelete('cascade');
            $table->foreignId('field_id')->constrained('fields')->onDelete('cascade');
            $table->foreignId('team1_id')->constrained('teams')->onDelete('cascade');
            $table->foreignId('team2_id')->constrained('teams')->onDelete('cascade');
            $table->string('status')->default('scheduled');
            $table->timestamps();
        });

        // Create break periods table
        Schema::create('break_periods', function (Blueprint $table) {
            $table->id();
            $table->foreignId('competition_id')->constrained('competitions')->onDelete('cascade');
            $table->foreignId('field_id')->constrained('fields')->onDelete('cascade');
            $table->string('name');
            $table->text('description')->nullable();
            $table->dateTime('start_time');
            $table->dateTime('end_time');
            $table->string('type')->default('break');
            $table->string('status')->default('planned');
            $table->integer('round')->default(1);
            $table->integer('match_slot')->default(1);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('break_periods');
        Schema::dropIfExists('matches');
        Schema::dropIfExists('competition_team');
        Schema::dropIfExists('teams');
        Schema::dropIfExists('fields');
        Schema::dropIfExists('scheduling_modes');
        Schema::dropIfExists('competitions');
    }
};
