<?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('space_types', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique();
            $table->text('description')->nullable();
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->index('is_active');
            $table->index('sort_order');
        });

        Schema::create('spaces', function (Blueprint $table) {
            $table->id();
            $table->foreignId('space_type_id')->constrained()->restrictOnDelete();
            $table->string('name');
            $table->string('code')->unique();
            $table->text('description')->nullable();
            $table->string('location')->nullable();
            $table->unsignedInteger('min_capacity')->nullable();
            $table->unsignedInteger('max_capacity')->nullable();
            $table->decimal('standard_rate', 15, 2)->nullable();
            $table->decimal('hourly_rate', 15, 2)->nullable();
            $table->decimal('half_day_rate', 15, 2)->nullable();
            $table->decimal('full_day_rate', 15, 2)->nullable();
            $table->unsignedSmallInteger('setup_duration_minutes')->default(0);
            $table->unsignedSmallInteger('breakdown_duration_minutes')->default(0);
            $table->time('opening_time')->nullable();
            $table->time('closing_time')->nullable();
            $table->text('amenities')->nullable();
            $table->text('equipment')->nullable();
            $table->text('terms')->nullable();
            $table->string('operational_status')->default('active');
            $table->timestamps();
            $table->softDeletes();

            $table->index('space_type_id');
            $table->index('operational_status');
            $table->index('name');
        });

        Schema::create('space_images', function (Blueprint $table) {
            $table->id();
            $table->foreignId('space_id')->constrained()->cascadeOnDelete();
            $table->string('path');
            $table->string('original_name')->nullable();
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->boolean('is_primary')->default(false);
            $table->timestamps();

            $table->index(['space_id', 'sort_order']);
        });

        Schema::create('space_layouts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('space_id')->constrained()->cascadeOnDelete();
            $table->string('layout_name');
            $table->unsignedInteger('capacity');
            $table->boolean('is_custom')->default(false);
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();

            $table->index(['space_id', 'sort_order']);
        });

        Schema::create('space_blocks', function (Blueprint $table) {
            $table->id();
            $table->foreignId('space_id')->constrained()->cascadeOnDelete();
            $table->string('block_type');
            $table->dateTime('starts_at');
            $table->dateTime('ends_at');
            $table->text('reason')->nullable();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index(['space_id', 'starts_at', 'ends_at']);
            $table->index('block_type');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('space_blocks');
        Schema::dropIfExists('space_layouts');
        Schema::dropIfExists('space_images');
        Schema::dropIfExists('spaces');
        Schema::dropIfExists('space_types');
    }
};
