# fhir_to_engine.py
from __future__ import annotations
from typing import Any, Dict, List

# Map LOINC -> TherapyEngine parameter keys
LOINC_TO_ENGINE_PARAM = {
    "8478-0": "MAP",
    "8867-4": "HR",
    "2708-6": "SpO2",
    "8310-5": "Temp",
    # Add more if you want:
    # "8462-4": "DBP",
    # "8480-6": "SBP",
}

def flattened_vitals_to_engine_readings(rows: List[Dict[str, Any]]) -> Dict[str, Dict[str, Any]]:
    """
    Convert your _flatten_vital_observation() rows into:
      { "MAP": {"value": 72, "id": "..."} , ... }
    """
    out: Dict[str, Dict[str, Any]] = {}
    for r in rows:
        code = r.get("loinc_code")
        if not code:
            continue
        param = LOINC_TO_ENGINE_PARAM.get(code)
        if not param:
            continue

        out[param] = {
            "value": float(r["value"]),
            "id": r.get("observation_id"),
        }
    return out
