<?php

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

/**
 * medward_bed_assignments — the bed history for an admission (SIFA P6-T2
 * bed_assignment). One open row (released_at NULL) per admission at a time;
 * AdmissionService closes the current row and opens a new one on transfer, and
 * closes it on discharge. reason captures the move rationale.
 */
return new class extends Migration {
    public function up(): void
    {
        if (Schema::hasTable('medward_bed_assignments')) {
            return;
        }

        Schema::create('medward_bed_assignments', function (Blueprint $t) {
            $t->bigIncrements('id');
            $t->unsignedBigInteger('admission_id');
            $t->unsignedBigInteger('bed_id');
            $t->timestamp('assigned_at')->nullable();
            $t->timestamp('released_at')->nullable();
            $t->string('reason', 255)->nullable();
            $t->timestamps();

            $t->index('admission_id', 'medward_ba_adm_idx');
            $t->index('bed_id', 'medward_ba_bed_idx');
        });
    }

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