# م6 (شاشة الطبيب بذاكرة) — عقد Fable + ليدجر (2026-07-07)

> العقد المُلزِم. الخريطة في `SCOUT.md`. المرجع `topics/clinic-dynamic-fields.md` (§3.1/3.2 أسماء الأعمدة **مجمّدة**). **اقرأ الليدجر أول حاجة عند العودة.**

## 📊 ليدجر
| WP | المحتوى | يعتمد | مراجعة | الحالة |
|----|---------|-------|--------|--------|
| WP1 | Engine schema (BE): migration أعمدة التعريف (unit/normal_min·max/danger_min·max/danger_values/is_computed/computed_key) + `value_num` على الإجابات + seed 9 vitals defs (ranges من VITAL_FIELDS، BMI computed) + HistoryService يملأ value_num + Resource يصدر is_danger/is_abnormal | — | native+Codex | ✅ |
| WP2 | Vitals fold-in (BE): backfill clinical_examinations→unified (idempotent only-if-absent) + VitalsService: unified=source of truth، mirror 9 codes للجدول الثابت (shim release واحد)، BMI computed_key + trend endpoint `GET patients/{id}/vitals-history?limit=5` | WP1 | native+Codex | ✅ |
| WP3 | Vitals FE: vitals-panel يرندر من التعريفات عبر DynamicFieldRenderer (numeric+unit+danger)، **احذف VITAL_FIELDS**، إصلاح bug null-clear (`?? undefined`→يبعت null)، ghost القيمة السابقة + مصغّر آخر 5 + badge خطر + soft-warn عند التوقيع | WP2 | single | ✅ |
| WP4 | History memory (BE): `EncounterHistoryController::show` يرجّع البنكين (encounter + patient بإجابات المريض + flag `carried` مشتق) + قلب أسئلة الحقائق المزمنة لـscope=patient في الseeder + `encounters.history_snapshot` json + listener على EncounterSigned + backfill آخر إجابة per-patient | WP1 | native+Codex | ✅ |
| WP5 | History FE + structured hx: history-panel يعرض patient-scope prefilled + chip «carried» + الكتابة لـsavePatientAnswers؛ توصيل add/remove للـfamily/medical/surgical (BE جاهز) | WP4 | single | ✅ |
| WP6a | Pick-first BE: `clinic_complaints`+`encounter_complaints` migration + CRUD-lite + inline-add؛ `icd10_codes` +nullable company_id+is_custom، قراءة scoped، custom-add endpoint (company_id صريح، غير-TenantAware محفوظ، prefix `C-`) | — | native+Codex | ✅ |
| WP6b | Pick-first FE: ودجت pick-multiselect مشترك + magic-Add (auto-select بلا reload) للشكوى (يستبدل textarea-primary، الحر يفضل note) + التشخيص (يستبدل typeahead)؛ bundle: إصلاح حذف التشخيص بلا refetch | WP6a | single | ✅ |
| WP7 | Previous-visits: aggregate endpoint `GET patients/{id}/visit-history?limit=10` (encounters newest-first بـcomplaints/diagnoses/rx/orders/documents/vitals_summary) + تبويب timeline في الكشف + «كرر آخر روشتة» | WP1 | single | ✅ |
| WP8 | Polish+ship: توحيد الtoast (host واحد)، ربط Alt+V/H/C/P HostListeners حقيقي (أو حذف الميت) + CHANGELOG + KB + نشر /app + push | الكل | single | ✅ |

**DAG:** {WP1→WP2→WP3} · {WP4→WP5 بعد WP1} · {WP6a→WP6b} · {WP7 بعد WP1}. WP8 آخرًا. ⚠️ **migrations (WP1/WP2/WP4/WP6a) تُسلسل على moonui_dev_be.**

## القرارات المُلزِمة (Fable)
- **1b — طيّ الvitals NOW:** unified `patient_history_answers` = مصدر الحقيقة لكل قراءات vitals؛ VitalsService يعكس الـ9 codes لأعمدة clinical_examinations (shim release واحد لأي طباعة/تقارير)؛ vitals مخصّصة جديدة في الموحّد فقط. BMI computed_key='bmi' يُكتب في الاتنين. (البديل الخفيف يفشل «أضيف مؤشر جديد يتخزّن».)
- **2 — pre-fill كامل:** الأربعة مع بعض (pre-fill + provenance + sign-snapshot + backfill) — بدون snapshot = ثغرة طبية-قانونية (تعديل المريض لبياناته لاحقًا يغيّر معنى زيارة موقّعة).
- **6 — magic-Add IN** (مع الكتابة الدائمة للكتالوج). icd10 كتالوج **عام** — ما يتعدّلش؛ customs صفوف company-scoped `C-`.
- **D1 danger:** مصدر واحد = صف التعريف؛ تقييم الجهتين (BE Resource is_danger/is_abnormal canonical + FE live)؛ 3 أسطح: field highlight · header badge · **soft-warn عند التوقيع (مش hard block في م6)**.
- **D2 provenance:** flag مشتق (`carried = answered_at < encounter.started_at`) — **مفيش عمود جديد**؛ السجل الثابت = `encounters.history_snapshot` json عند التوقيع. backfill: أحدث إجابة encounter-scoped لكل (patient,question) كصف patient-scoped (idempotent only-if-absent).
- **D3 previous-visits:** call واحد، aggregation server-side، limit≤25، read-only.
- **D4 chief complaint = كتالوج مسطّح + pivot** (`clinic_complaints`/`encounter_complaints`)، **مش history_question**؛ عمود `chief_complaint` النصي يفضل كـnote حر. diagnosis magic-Add: `icd10_codes` +company_id+is_custom nullable (العام NULL)، قراءة `company_id IS NULL OR = ?`. **ودجت FE مشترك واحد** للاتنين.
- **D5 trend:** `GET patients/{id}/vitals-history?limit=5` من الموحّد؛ ghost + مصغّر آخر 5.

## مؤجّلات م6 (Fable)
- preset packs تخصّصية (obgy/peds) + export/import → **م9**. admin CRUD للحقول → **م9**. إسقاط `clinical_examinations` → **release بعد م6**. max-visibility 5-col grid للـorders → **م9/orders-polish**. print pack → مرحلة لاحقة. hard-gate danger → flag م9.
- **قيد مُلزِم:** أسماء أعمدة §3.1/3.2 مجمّدة (WP1 يطابق الوثيقة حرفيًا). مدّد HistoryService/DynamicFieldRenderer — أي «أبني renderer/service جديد» = خرق نطاق.
