using StackExchange.Redis;

public static class GcsCalculator
{
    public static readonly IReadOnlyList<string> ComponentCodes = new[]
    {
        "GCS_EYE", "GCS_VERBAL", "GCS_MOTOR"
    };

    public static readonly IReadOnlySet<string> ComponentCodeSet =
        new HashSet<string>(ComponentCodes);

    public static bool IsGcsCode(string observationCode) =>
        ComponentCodeSet.Contains(observationCode);

    public static RedisKey[] AllComponentKeys(Guid encounterId) =>
        ComponentCodes
            .Select(code => (RedisKey)$"gcs:{encounterId}:{code}")
            .ToArray();

    public static string ComponentKey(Guid encounterId, string code) =>
        $"gcs:{encounterId}:{code}";

    // Compute total from three components. Returns null if any component missing.
    public static int? ComputeTotal(decimal? eye, decimal? verbal, decimal? motor)
    {
        if (eye is null || verbal is null || motor is null)
            return null;
        return (int)(eye.Value + verbal.Value + motor.Value);
    }

    // GCS severity classification
    public static string ClassifyGcs(int total) => total switch
    {
        <= 8  => "SEVERE",      // Coma
        <= 12 => "MODERATE",
        _     => "MILD"         // 13-15
    };

    // Map GCS total to NEWS2 consciousness score (replaces AVPU mapping)
    public static int ToNews2ConsciousnessScore(int gcsTotal) => gcsTotal switch
    {
        15 => 0,  // Fully alert — equivalent to AVPU=Alert
        _  => 3   // Any deficit — equivalent to AVPU=Voice/Pain/Unresponsive
    };

    // Map GCS total to qSOFA altered mentation criterion
    public static bool MeetsQsofaAlteredMentation(int gcsTotal) =>
        gcsTotal < 15;

    // Map GCS total to SOFA CNS score (used in Phase 26)
    public static int ToSofaCnsScore(int gcsTotal) => gcsTotal switch
    {
        15          => 0,
        >= 13       => 1,   // 13-14
        >= 10       => 2,   // 10-12
        >= 6        => 3,   // 6-9
        _           => 4    // < 6
    };
}