<?php

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

/**
 * medward_wards — an inpatient ward (SIFA P6-T1 bed_management → ward). A ward
 * optionally lives at a business location (business_location_id) and may link a
 * MedOrganization clc_department (dept_id) — links only, we never modify those
 * tables. business_id-scoped; references into ERP / sibling-module tables are
 * plain indexed columns with no cross-table FK constraints (module convention).
 */
return new class extends Migration {
    public function up(): void
    {
        if (Schema::hasTable('medward_wards')) {
            return;
        }

        Schema::create('medward_wards', function (Blueprint $t) {
            $t->bigIncrements('id');
            $t->unsignedInteger('business_id');
            $t->unsignedInteger('business_location_id')->nullable(); // ERP business_locations.id
            $t->unsignedInteger('dept_id')->nullable();              // MedOrganization clc_departments.id (link only)
            $t->string('name', 100);
            $t->string('code', 30)->nullable();
            $t->enum('ward_type', [
                'general', 'icu', 'nicu', 'picu', 'maternity',
                'surgical', 'medical', 'isolation', 'other',
            ])->default('general');
            $t->string('floor', 30)->nullable();
            $t->integer('total_beds')->default(0);
            $t->unsignedInteger('in_charge_user_id')->nullable();    // users.id (in-charge nurse / doctor)
            $t->text('notes')->nullable();
            $t->boolean('is_active')->default(true);
            $t->timestamps();

            $t->index(['business_id', 'is_active'], 'medward_wards_biz_active_idx');
            $t->index(['business_id', 'ward_type'], 'medward_wards_biz_type_idx');
            $t->unique(['business_id', 'code'], 'medward_wards_biz_code_uq');
        });
    }

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