public enum AlertType
{
    [Obsolete("Legacy — replaced by SOFA_SEPSIS in Phase 27. Retained for historical alert queries.")]
    SepsisWarning,
    CriticalHeartRate,
    CriticalTempC,
    CriticalPotassiumMeqL,
    CriticalSpo2,
    CriticalRespRate,
    CriticalWbcKUl,
    CriticalSystolicBp,
    CriticalDiastolicBp,
    CriticalLactateMmolL,
    CriticalAvpu,
    CriticalGlucoseMgDl,

    // New — warning-level threshold alerts
    WarningHeartRate,
    WarningTempC,
    WarningPotassiumMeqL,
    WarningSpo2,
    WarningRespRate,
    WarningWbcKUl,
    WarningSystolicBp,
    WarningDiastolicBp,
    WarningLactateMmolL,
    WarningGlucoseMgDl,

    News2Warning,
    News2Emergency,

    RapidDeterioration,

    [Obsolete("Legacy — replaced by QSOFA_SCREEN in Phase 27. Retained for historical alert queries.")]
    QsofaWarning,

    QsofaScreen,

    GcsCritical,
    GcsWarning,

    CriticalPao2MmHg,
    WarningPao2MmHg,
    CriticalPlateletKUl,
    WarningPlateletKUl,
    CriticalBilirubinMgDl,
    WarningBilirubinMgDl,
    CriticalCreatinineMgDl,
    WarningCreatinineMgDl,

    SofaSepsis,
    SofaWarning,
}

public static class AlertTypeExtensions
{
#pragma warning disable CS0618 // Legacy alert types retained for historical DB strings
    public static string ToDbString(this AlertType t) => t switch
    {
        AlertType.SepsisWarning         => "SEPSIS_WARNING",
        AlertType.CriticalHeartRate     => "CRITICAL_HEART_RATE",
        AlertType.CriticalTempC         => "CRITICAL_TEMP_C",
        AlertType.CriticalPotassiumMeqL => "CRITICAL_POTASSIUM_MEQ_L",
        AlertType.CriticalSpo2          => "CRITICAL_SPO2",
        AlertType.CriticalRespRate      => "CRITICAL_RESP_RATE",
        AlertType.CriticalWbcKUl        => "CRITICAL_WBC_K_UL",
        AlertType.CriticalSystolicBp    => "CRITICAL_SYSTOLIC_BP",
        AlertType.CriticalDiastolicBp   => "CRITICAL_DIASTOLIC_BP",
        AlertType.CriticalLactateMmolL  => "CRITICAL_LACTATE_MMOL_L",
        AlertType.CriticalAvpu          => "CRITICAL_AVPU",
        AlertType.CriticalGlucoseMgDl   => "CRITICAL_GLUCOSE_MG_DL",
        AlertType.WarningHeartRate      => "WARNING_HEART_RATE",
        AlertType.WarningTempC          => "WARNING_TEMP_C",
        AlertType.WarningPotassiumMeqL  => "WARNING_POTASSIUM_MEQ_L",
        AlertType.WarningSpo2           => "WARNING_SPO2",
        AlertType.WarningRespRate       => "WARNING_RESP_RATE",
        AlertType.WarningWbcKUl         => "WARNING_WBC_K_UL",
        AlertType.WarningSystolicBp     => "WARNING_SYSTOLIC_BP",
        AlertType.WarningDiastolicBp    => "WARNING_DIASTOLIC_BP",
        AlertType.WarningLactateMmolL   => "WARNING_LACTATE_MMOL_L",
        AlertType.WarningGlucoseMgDl    => "WARNING_GLUCOSE_MG_DL",
        AlertType.News2Warning   => "NEWS2_WARNING",
        AlertType.News2Emergency => "NEWS2_EMERGENCY",
        AlertType.RapidDeterioration => "RAPID_DETERIORATION",
        AlertType.QsofaWarning => "QSOFA_WARNING",
        AlertType.GcsCritical => "GCS_CRITICAL",
        AlertType.GcsWarning  => "GCS_WARNING",
        AlertType.CriticalPao2MmHg       => "CRITICAL_PAO2_MMHG",
        AlertType.WarningPao2MmHg        => "WARNING_PAO2_MMHG",
        AlertType.CriticalPlateletKUl    => "CRITICAL_PLATELET_K_UL",
        AlertType.WarningPlateletKUl     => "WARNING_PLATELET_K_UL",
        AlertType.CriticalBilirubinMgDl  => "CRITICAL_BILIRUBIN_MG_DL",
        AlertType.WarningBilirubinMgDl   => "WARNING_BILIRUBIN_MG_DL",
        AlertType.CriticalCreatinineMgDl => "CRITICAL_CREATININE_MG_DL",
        AlertType.WarningCreatinineMgDl  => "WARNING_CREATININE_MG_DL",
        AlertType.SofaSepsis  => "SOFA_SEPSIS",
        AlertType.SofaWarning => "SOFA_WARNING",
        AlertType.QsofaScreen => "QSOFA_SCREEN",
        _ => throw new ArgumentOutOfRangeException(nameof(t))
    };
#pragma warning restore CS0618

#pragma warning disable CS0618 // Legacy alert types retained for historical DB strings
    public static AlertType FromDbString(string v) => v switch
    {
        "SEPSIS_WARNING"          => AlertType.SepsisWarning,
        "CRITICAL_HEART_RATE"     => AlertType.CriticalHeartRate,
        "CRITICAL_TEMP_C"         => AlertType.CriticalTempC,
        "CRITICAL_POTASSIUM_MEQ_L"=> AlertType.CriticalPotassiumMeqL,
        "CRITICAL_SPO2"           => AlertType.CriticalSpo2,
        "CRITICAL_RESP_RATE"      => AlertType.CriticalRespRate,
        "CRITICAL_WBC_K_UL"       => AlertType.CriticalWbcKUl,
        "CRITICAL_SYSTOLIC_BP"    => AlertType.CriticalSystolicBp,
        "CRITICAL_DIASTOLIC_BP"   => AlertType.CriticalDiastolicBp,
        "CRITICAL_LACTATE_MMOL_L" => AlertType.CriticalLactateMmolL,
        "CRITICAL_AVPU"           => AlertType.CriticalAvpu,
        "CRITICAL_GLUCOSE_MG_DL"  => AlertType.CriticalGlucoseMgDl,
        "WARNING_HEART_RATE"      => AlertType.WarningHeartRate,
        "WARNING_TEMP_C"          => AlertType.WarningTempC,
        "WARNING_POTASSIUM_MEQ_L" => AlertType.WarningPotassiumMeqL,
        "WARNING_SPO2"            => AlertType.WarningSpo2,
        "WARNING_RESP_RATE"       => AlertType.WarningRespRate,
        "WARNING_WBC_K_UL"        => AlertType.WarningWbcKUl,
        "WARNING_SYSTOLIC_BP"     => AlertType.WarningSystolicBp,
        "WARNING_DIASTOLIC_BP"    => AlertType.WarningDiastolicBp,
        "WARNING_LACTATE_MMOL_L"  => AlertType.WarningLactateMmolL,
        "WARNING_GLUCOSE_MG_DL"   => AlertType.WarningGlucoseMgDl,
        "NEWS2_WARNING"   => AlertType.News2Warning,
        "NEWS2_EMERGENCY" => AlertType.News2Emergency,
        "RAPID_DETERIORATION" => AlertType.RapidDeterioration,
        "QSOFA_WARNING" => AlertType.QsofaWarning,
        "QSOFA_SCREEN" => AlertType.QsofaScreen,
        "GCS_CRITICAL" => AlertType.GcsCritical,
        "GCS_WARNING"  => AlertType.GcsWarning,
        "CRITICAL_PAO2_MMHG"        => AlertType.CriticalPao2MmHg,
        "WARNING_PAO2_MMHG"         => AlertType.WarningPao2MmHg,
        "CRITICAL_PLATELET_K_UL"    => AlertType.CriticalPlateletKUl,
        "WARNING_PLATELET_K_UL"     => AlertType.WarningPlateletKUl,
        "CRITICAL_BILIRUBIN_MG_DL"  => AlertType.CriticalBilirubinMgDl,
        "WARNING_BILIRUBIN_MG_DL"   => AlertType.WarningBilirubinMgDl,
        "CRITICAL_CREATININE_MG_DL" => AlertType.CriticalCreatinineMgDl,
        "WARNING_CREATININE_MG_DL"  => AlertType.WarningCreatinineMgDl,
        "SOFA_SEPSIS"  => AlertType.SofaSepsis,
        "SOFA_WARNING" => AlertType.SofaWarning,
        _ => throw new ArgumentOutOfRangeException(nameof(v), $"Unknown alert type: '{v}'")
    };
#pragma warning restore CS0618

    // Threshold alerts are derived from observation codes in alert_thresholds — not free-form strings.
    public static AlertType CriticalFor(string observationCode) => observationCode switch
    {
        "HEART_RATE"       => AlertType.CriticalHeartRate,
        "TEMP_C"           => AlertType.CriticalTempC,
        "POTASSIUM_MEQ_L"  => AlertType.CriticalPotassiumMeqL,
        "SPO2"             => AlertType.CriticalSpo2,
        "RESP_RATE"        => AlertType.CriticalRespRate,
        "WBC_K_UL"         => AlertType.CriticalWbcKUl,
        "SYSTOLIC_BP"      => AlertType.CriticalSystolicBp,
        "DIASTOLIC_BP"     => AlertType.CriticalDiastolicBp,
        "LACTATE_MMOL_L"   => AlertType.CriticalLactateMmolL,
        "AVPU"             => AlertType.CriticalAvpu,
        "GLUCOSE_MG_DL"    => AlertType.CriticalGlucoseMgDl,
        "PAO2_MMHG"        => AlertType.CriticalPao2MmHg,
        "PLATELET_K_UL"    => AlertType.CriticalPlateletKUl,
        "BILIRUBIN_MG_DL"  => AlertType.CriticalBilirubinMgDl,
        "CREATININE_MG_DL" => AlertType.CriticalCreatinineMgDl,
        _ => throw new ArgumentOutOfRangeException(
            nameof(observationCode), $"No critical alert type for observation code '{observationCode}'")
    };

    public static AlertType WarningFor(string observationCode) => observationCode switch
    {
        "HEART_RATE"      => AlertType.WarningHeartRate,
        "TEMP_C"          => AlertType.WarningTempC,
        "POTASSIUM_MEQ_L" => AlertType.WarningPotassiumMeqL,
        "SPO2"            => AlertType.WarningSpo2,
        "RESP_RATE"       => AlertType.WarningRespRate,
        "WBC_K_UL"        => AlertType.WarningWbcKUl,
        "SYSTOLIC_BP"     => AlertType.WarningSystolicBp,
        "DIASTOLIC_BP"    => AlertType.WarningDiastolicBp,
        "LACTATE_MMOL_L"  => AlertType.WarningLactateMmolL,
        "GLUCOSE_MG_DL"   => AlertType.WarningGlucoseMgDl,
        "PAO2_MMHG"        => AlertType.WarningPao2MmHg,
        "PLATELET_K_UL"    => AlertType.WarningPlateletKUl,
        "BILIRUBIN_MG_DL"  => AlertType.WarningBilirubinMgDl,
        "CREATININE_MG_DL" => AlertType.WarningCreatinineMgDl,
        _ => throw new ArgumentOutOfRangeException(
            nameof(observationCode), $"No warning alert type for observation code '{observationCode}'")
    };

#pragma warning disable CS0618 // Legacy alert types retained for historical DB strings
    public static bool IsSuppressible(this AlertType t) => t switch
    {
        AlertType.SepsisWarning or AlertType.News2Emergency => false,
        AlertType.CriticalHeartRate or AlertType.CriticalTempC or AlertType.CriticalPotassiumMeqL
            or AlertType.CriticalSpo2 or AlertType.CriticalRespRate or AlertType.CriticalWbcKUl
            or AlertType.CriticalSystolicBp or AlertType.CriticalDiastolicBp
            or AlertType.CriticalLactateMmolL or AlertType.CriticalAvpu
            or AlertType.CriticalGlucoseMgDl => false,
        AlertType.RapidDeterioration => false, 
        AlertType.GcsCritical => false, // trajectory alerts are never suppressed
        AlertType.SofaSepsis => false,
        _ => true  // all Warning* types, News2Warning, QsofaScreen, SofaWarning, GcsWarning
    };
#pragma warning restore CS0618

    public static string? ObservationCodeForWarning(this AlertType t) => t switch
    {
        AlertType.WarningHeartRate      => "HEART_RATE",
        AlertType.WarningTempC          => "TEMP_C",
        AlertType.WarningPotassiumMeqL  => "POTASSIUM_MEQ_L",
        AlertType.WarningSpo2           => "SPO2",
        AlertType.WarningRespRate       => "RESP_RATE",
        AlertType.WarningWbcKUl         => "WBC_K_UL",
        AlertType.WarningSystolicBp     => "SYSTOLIC_BP",
        AlertType.WarningDiastolicBp    => "DIASTOLIC_BP",
        AlertType.WarningLactateMmolL   => "LACTATE_MMOL_L",
        AlertType.WarningGlucoseMgDl    => "GLUCOSE_MG_DL",
        AlertType.WarningPao2MmHg        => "PAO2_MMHG",
        AlertType.WarningPlateletKUl     => "PLATELET_K_UL",
        AlertType.WarningBilirubinMgDl   => "BILIRUBIN_MG_DL",
        AlertType.WarningCreatinineMgDl  => "CREATININE_MG_DL",
        _ => null
    };
}
