<?php

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

/**
 * mder_visit_events — an insert-only ER timeline (SIFA §9 emergency).
 *
 * One row per state change / clinical action on a visit (arrived, triaged, seen,
 * order, result, disposition, note). Rows are never updated or deleted, so the
 * timeline is an auditable history. created_at is set explicitly (the model has
 * no updated_at).
 */
return new class extends Migration {
    public function up(): void
    {
        if (Schema::hasTable('mder_visit_events')) {
            return;
        }

        Schema::create('mder_visit_events', function (Blueprint $t) {
            $t->bigIncrements('id');
            $t->unsignedBigInteger('visit_id');
            $t->enum('event', ['arrived', 'triaged', 'seen', 'order', 'result', 'disposition', 'note']);
            $t->text('detail')->nullable();
            $t->unsignedInteger('user_id')->nullable();          // users.id
            $t->timestamp('created_at')->nullable();

            $t->index('visit_id', 'mder_visit_events_visit_idx');
            $t->index(['visit_id', 'event'], 'mder_visit_events_visit_event_idx');
        });
    }

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