# MedEmergency (SIFA P6-T3)

Emergency department (ER) module — arrival, triage, a live ER board and
disposition — built entirely as a nwidart module over **ClinicCore**. The ERP
core stays byte-identical; only `Modules/MedEmergency` writes.

## What it does

- **Arrivals (quick-reg).** Name is enough to get on the board. Optionally
  links/creates an ERP `App\Contact` + a `clc_patients` row (mirrors
  `ClcPatientController`), so a walk-in becomes a real patient record when you
  want it. `patient_id` / `contact_id` stay nullable so triage is never blocked.
- **Triage.** A structured form with a **live auto-suggestion** driven by vitals:
  - `GCS < 9` → **resuscitation**
  - `RR > 30` **or** `SpO2 < 90` **or** `HR > 130` → **emergent**
  Saving writes a real ClinicCore `clc_vitals` row (`appointment_id` null — an
  ad-hoc ER reading), stamps the triage fields, advances the board and — for
  resuscitation / emergent — raises a **critical `MedicalAlert`** (fail-soft).
- **ER board.** Columns by `board_status`, triage-level colour coding, a ticking
  time-in-department, drag-less status buttons, a LWBS quick action, a today
  metrics strip (door-to-triage median, door-to-doctor median, LWBS count) and a
  30s auto-refresh. A visit detail drawer shows the timeline + triage + vitals +
  disposition.
- **Disposition.** `admitted` hands off to **MedWard** when available (creates a
  `medward_admissions` row + deep-link, board → `admitted_awaiting_bed`);
  otherwise the disposition is simply recorded. Every other disposition closes
  the visit (board → `done`).

## Tables (prefix `mder_`)

- `mder_visits` — one ER visit (`visit_no` = `ER-{biz}-{seq}`).
- `mder_visit_events` — insert-only ER timeline.

Migrations are `2026_07_05_6xxx`. `business_id` on every row.

## Reuse & fail-soft boundaries

- **ClinicCore** — patients/contacts, `clc_vitals` (triage vitals),
  `MedicalAlertService` (critical alerts). Hard dependency (`requires`).
- **EmergencyClinic** `emergency_patient_profiles` — legacy triage fields are
  **read-only**; this module never edits that table.
- **MedWard** `medward_admissions` — the admit hand-off is **strictly fail-soft**
  and NEVER references a MedWard class; it probes `Schema::hasTable` /
  `Schema::hasColumn` and `Route::has`. MedWard is being built in parallel and is
  never required.

## Permissions

`mder.triage`, `mder.board.view`, `mder.disposition`.

## Install

Superadmin → Modules → install `MedEmergency` (or hit `/med-emergency/install`).
The install gate is the lowercase `medemergency_version` system property.
