<?php

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

/**
 * Rooms (spec §5 room; REUSE-MAP §5 "build-new"), FK to ClinicCore
 * clc_departments.
 */
return new class extends Migration {
    public function up(): void
    {
        if (Schema::hasTable('org_rooms')) {
            return;
        }

        Schema::create('org_rooms', function (Blueprint $t) {
            $t->bigIncrements('id');
            $t->unsignedInteger('business_id');
            $t->unsignedInteger('department_id');
            $t->string('name', 100);
            $t->string('code', 20)->nullable();
            $t->enum('room_type', [
                'consultation', 'operating', 'emergency', 'lab', 'imaging',
                'pharmacy', 'waiting', 'admin', 'storage', 'other',
            ])->default('other');
            $t->integer('floor')->nullable();
            $t->integer('capacity')->nullable();
            $t->json('equipment')->nullable();
            $t->enum('status', ['available', 'occupied', 'maintenance', 'reserved'])->default('available');
            $t->boolean('scheduling_enabled')->default(false);
            $t->timestamps();

            $t->foreign('business_id')->references('id')->on('business')->onDelete('cascade');
            $t->foreign('department_id')->references('id')->on('clc_departments')->onDelete('cascade');
            $t->index(['business_id', 'department_id']);
            $t->index(['business_id', 'room_type']);
            $t->index(['business_id', 'status']);
        });
    }

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