<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('facilities', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('academy_id')->constrained('academies');
            $table->foreignId('branch_id')->constrained('branches');
            $table->string('name', 255);
            $table->string('name_ar', 255);
            $table->string('code', 20)->nullable();
            $table->string('type', 50);
            $table->string('status', 30)->default('active');
            $table->text('description')->nullable();
            $table->text('description_ar')->nullable();
            $table->integer('capacity')->nullable();
            $table->string('surface_type', 50)->nullable();
            $table->decimal('area_sqm', 8, 2)->nullable();
            $table->decimal('length_m', 6, 2)->nullable();
            $table->decimal('width_m', 6, 2)->nullable();
            $table->boolean('is_indoor')->default(false);
            $table->boolean('has_lighting')->default(true);
            $table->boolean('has_ac')->default(false);
            $table->time('operating_start')->nullable();
            $table->time('operating_end')->nullable();
            $table->bigInteger('rental_cost_per_hour')->nullable();
            $table->string('address')->nullable();
            $table->decimal('latitude', 10, 7)->nullable();
            $table->decimal('longitude', 10, 7)->nullable();
            $table->jsonb('amenities')->default('[]');
            $table->jsonb('metadata')->default('{}');
            $table->string('photo_path')->nullable();
            $table->integer('sort_order')->default(0);
            $table->foreignId('created_by')->constrained('users');
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['academy_id', 'code']);
            $table->index(['academy_id', 'status']);
            $table->index(['branch_id', 'status']);
        });

        DB::statement("ALTER TABLE facilities ADD CONSTRAINT facilities_status_check CHECK (status IN ('active', 'maintenance', 'closed', 'reserved', 'unavailable'))");
        DB::statement("ALTER TABLE facilities ADD CONSTRAINT facilities_type_check CHECK (type IN ('field', 'court', 'pool', 'gym', 'track', 'hall', 'room', 'outdoor', 'other'))");

        // Add FK constraints to existing tables that already have the facility_id column
        Schema::table('training_schedules', function (Blueprint $table) {
            $table->foreign('facility_id')->references('id')->on('facilities')->nullOnDelete();
        });

        Schema::table('training_sessions', function (Blueprint $table) {
            $table->foreign('facility_id')->references('id')->on('facilities')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('training_sessions', function (Blueprint $table) {
            $table->dropForeign(['facility_id']);
        });
        Schema::table('training_schedules', function (Blueprint $table) {
            $table->dropForeign(['facility_id']);
        });
        Schema::dropIfExists('facilities');
    }
};
