<?php

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

/**
 * medward_fluid_balance — per-shift intake / output for an admission (SIFA P6-T2
 * fluid_balance). Individual intake_* / output_* volumes (ml) are stored; the net
 * balance is computed on the fly by the model accessor (never persisted), so a
 * corrected line always yields a consistent balance.
 */
return new class extends Migration {
    public function up(): void
    {
        if (Schema::hasTable('medward_fluid_balance')) {
            return;
        }

        Schema::create('medward_fluid_balance', function (Blueprint $t) {
            $t->bigIncrements('id');
            $t->unsignedInteger('business_id');
            $t->unsignedBigInteger('admission_id');
            $t->timestamp('recorded_at')->nullable();
            $t->enum('shift', ['morning', 'evening', 'night']);
            $t->decimal('intake_oral', 10, 2)->nullable();
            $t->decimal('intake_iv', 10, 2)->nullable();
            $t->decimal('intake_other', 10, 2)->nullable();
            $t->decimal('output_urine', 10, 2)->nullable();
            $t->decimal('output_drain', 10, 2)->nullable();
            $t->decimal('output_other', 10, 2)->nullable();
            $t->unsignedInteger('recorded_by')->nullable();          // users.id
            $t->timestamps();

            $t->index(['business_id', 'admission_id', 'recorded_at'], 'medward_fb_biz_adm_at_idx');
        });
    }

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