"""
scoring.py
Combines per-category scores (0-100) into a final Growth Score (0-100)
based on each category's weight.
"""
from rule_engine import CATEGORY_WEIGHTS, CATEGORY_LABELS


def compute_growth_score(category_scores: dict) -> dict:
    total = 0.0
    for cat, weight in CATEGORY_WEIGHTS.items():
        total += category_scores.get(cat, 0) * weight

    growth_score = round(total)

    breakdown = [
        {
            "category": cat,
            "label": CATEGORY_LABELS[cat],
            "score": category_scores.get(cat, 0),
            "weight": weight,
        }
        for cat, weight in CATEGORY_WEIGHTS.items()
    ]
    breakdown.sort(key=lambda x: x["score"])

    if growth_score >= 85:
        grade = "Excellent"
    elif growth_score >= 70:
        grade = "Good"
    elif growth_score >= 50:
        grade = "Needs Improvement"
    else:
        grade = "Needs Serious Attention"

    return {
        "growth_score": growth_score,
        "grade": grade,
        "category_breakdown": breakdown,
    }
