<?php

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

/**
 * Visit-scoped vitals for ward nursing (no consultation required).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('care_visit_vitals', function (Blueprint $table) {
            $table->id();
            $table->string('owner_ref')->index();
            $table->foreignId('organization_id')->constrained('care_organizations')->cascadeOnDelete();
            $table->foreignId('visit_id')->constrained('care_visits')->cascadeOnDelete();
            $table->foreignId('patient_id')->constrained('care_patients')->cascadeOnDelete();
            $table->foreignId('care_unit_id')->nullable()->constrained('care_care_units')->nullOnDelete();
            $table->unsignedSmallInteger('bp_systolic')->nullable();
            $table->unsignedSmallInteger('bp_diastolic')->nullable();
            $table->unsignedSmallInteger('pulse')->nullable();
            $table->decimal('temperature', 4, 1)->nullable();
            $table->unsignedSmallInteger('spo2')->nullable();
            $table->unsignedSmallInteger('respiratory_rate')->nullable();
            $table->string('recorded_by')->nullable();
            $table->timestamp('recorded_at');
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->index(['visit_id', 'recorded_at']);
            $table->index(['care_unit_id', 'recorded_at']);
        });
    }

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