"""
Clinical ranges for health metrics with programmatic status and score calculation.
"""

def parse_blood_pressure(bp_str):
    """Parse blood pressure string like '120/80' into systolic/diastolic"""
    try:
        parts = bp_str.split('/')
        return int(parts[0]), int(parts[1])
    except:
        return 120, 80  # Default if parsing fails

def get_metric_evaluation(metric_name, value):
    """
    Returns (status, score) for a given metric and value.
    Status: Normal, High, Low, Optimal, Borderline, Critical
    Score: 0-10 based on clinical ranges
    """
    
    evaluations = {
        "heart_rate": lambda v: (
            ("Normal", 10) if 51 <= v <= 100 else
            ("Borderline Low", 7) if v == 50 else
            ("Borderline High", 7) if 100 < v <= 110 else
            ("Low", 3) if v < 50 else ("High", 3)
        ),
        "breathing_rate": lambda v: (
            ("Normal", 10) if 12 <= v <= 20 else
            ("Borderline Low", 7) if 10 <= v < 12 else
            ("Borderline High", 7) if 20 < v <= 24 else
            ("Low", 3) if v < 10 else ("High", 3)
        ),
        "oxygen_saturation": lambda v: (
            ("Normal", 10) if v >= 94 else
            ("Borderline Low", 7) if 91 <= v < 94 else
            ("Low", 4) if 88 <= v < 91 else
            ("Critically Low", 0)
        ),
        "blood_pressure": lambda v: (
            (lambda s, d: (
                # Get individual statuses
                (lambda sys_status, dia_status, score: (
                    f"Systolic {sys_status}, Diastolic {dia_status}", score
                ))(
                    # Systolic status
                    "Crisis" if s > 180 else
                    "Stage 2 High" if s >= 140 else
                    "Stage 1 High" if 130 <= s < 140 else
                    "Elevated" if 120 <= s < 130 else
                    "Normal",
                    # Diastolic status
                    "Crisis" if d > 120 else
                    "Stage 2 High" if d >= 90 else
                    "Stage 1 High" if 80 <= d < 90 else
                    "Normal",
                    # Score (use worst case)
                    0 if s > 180 or d > 120 else
                    3 if s >= 140 or d >= 90 else
                    6 if 130 <= s < 140 or 80 <= d < 90 else
                    8 if 120 <= s < 130 else
                    10
                )
            ))(*parse_blood_pressure(v)) if isinstance(v, str) else ("Systolic Normal, Diastolic Normal", 10)
        ),
        "stress_level": lambda v: (
            ("Normal", 10) if v in ["Low", "Normal"] else
            ("Mild", 7) if v == "Mild" else
            ("High", 4) if v == "High" else
            ("Very High", 2)
        ),
        "heart_variability": lambda v: (
            ("Normal", 10) if 50 <= v <= 100 else
            ("Borderline Low", 7) if 30 <= v < 50 else
            ("Borderline High", 7) if 100 < v <= 150 else
            ("Low", 4) if v < 30 else ("High", 4)
        ),
        "prq": lambda v: (
            ("Optimal", 10) if 80 <= v <= 100 else
            ("Normal", 7) if 60 <= v < 80 else
            ("Low", 4)
        ),
        "activity": lambda v: (
            ("Optimal", 10) if v >= 7 else
            ("Good", 7) if 5 <= v < 7 else
            ("Fair", 4) if 3 <= v < 5 else
            ("Low", 2)
        ),
        "sleep": lambda v: (
            ("Optimal", 10) if 7 <= v <= 9 else
            ("Good", 7) if 6 <= v < 7 or 9 < v <= 10 else
            ("Poor", 4)
        ),
        "equilibrium": lambda v: (
            ("Optimal", 5) if v >= 7 else
            ("Good", 4) if 5 <= v < 7 else
            ("Fair", 3) if 3 <= v < 5 else
            ("Low", 1)
        ),
        "metabolism": lambda v: (
            ("Optimal", 10) if v >= 7 else
            ("Good", 7) if 5 <= v < 7 else
            ("Fair", 4) if 3 <= v < 5 else
            ("Low", 2)
        ),
        "relaxation": lambda v: (
            ("Optimal", 10) if v >= 7 else
            ("Good", 7) if 5 <= v < 7 else
            ("Fair", 4) if 3 <= v < 5 else
            ("Low", 2)
        ),
        "cardiovascular_age": lambda v: (
            ("Optimal", 10) if v <= 35 else
            ("Good", 7) if v <= 45 else
            ("Fair", 4) if v <= 55 else
            ("High", 2)
        ),
        "hemoglobin": lambda v: (
            ("Normal", 10) if 13.5 <= v <= 17.5 else
            ("Borderline Low", 7) if 12 <= v < 13.5 else
            ("Borderline High", 7) if 17.5 < v <= 19 else
            ("Low", 3) if v < 12 else ("High", 3)
        ),
        "cholesterol": lambda v: (
            ("Normal", 10) if v < 200 else
            ("Borderline High", 6) if 200 <= v < 240 else
            ("High", 2)
        ),
        "a1c_risk": lambda v: (
            ("Normal", 10) if v < 5.7 else
            ("Prediabetic", 6) if 5.7 <= v < 6.5 else
            ("Diabetic", 2)
        ),
        "cholesterol_risk": lambda v: (
            ("Normal", 10) if v < 5 else
            ("Borderline High", 7) if 5 <= v < 7.5 else
            ("Intermediate", 4) if 7.5 <= v < 20 else
            ("High", 2)
        ),
        "wellness_score": lambda v: (
            ("Optimal", 10) if v >= 8 else
            ("Good", 8) if 6 <= v < 8 else
            ("Fair", 5) if 4 <= v < 6 else
            ("Poor", 2)
        ),
        "atrial_fibrillation": lambda v: (
            ("Normal", 10) if v == "NSR" else
            ("Abnormal", 3) if v == "AFib" else
            ("Error", 0)
        ),
        "hba1c": lambda v: (
            ("Normal", 10) if v < 5.7 else
            ("Prediabetic", 6) if 5.7 <= v < 6.5 else
            ("Diabetic", 2)
        ),
        "cardiovascular_bmi": lambda v: (
            ("Normal", 10) if 18.5 <= v <= 24.9 else
            ("Underweight", 7) if v < 18.5 else
            ("Overweight", 7) if 25.0 <= v < 30.0 else
            ("Obese", 3)
        ),
        "a1c_range": lambda v: ("Normal", 10),  # Display range, default to Normal/10 if present
        "cholesterol_range": lambda v: (
            ("Normal", 10) if v == "Normal" else
            ("Borderline High", 6) if v == "Borderline High" else
            ("High", 2) if v == "High" else
            ("Normal", 10)
        ),
    }
    
    # Get the evaluation function for this metric
    eval_func = evaluations.get(metric_name.lower())
    if eval_func:
        try:
            return eval_func(value)
        except:
            return ("Normal", 5)  # Default on error
    return ("Normal", 5)  # Default for unknown metrics


def evaluate_all_metrics(data_dict):
    """
    Evaluate all metrics from input data.
    Returns dict of {metric_name: {"status": str, "score": int}}
    """
    results = {}
    for key, value in data_dict.items():
        if value is not None:
            status, score = get_metric_evaluation(key, value)
            results[key] = {"status": status, "score": score}
    return results
