"""
simulator.py
Menghitung ulang Growth Score jika pengguna memilih menerapkan
sejumlah rekomendasi tertentu. Bukan prediksi presisi, melainkan
estimasi relatif berdasarkan bobot impact tiap rekomendasi.
"""
from rule_engine import CATEGORY_WEIGHTS
from scoring import compute_growth_score

IMPACT_POINTS = {"high": 20, "medium": 10, "low": 5}


def simulate(category_scores: dict, all_recommendations: list[dict], selected_ids: list[str]) -> dict:
    simulated_scores = dict(category_scores)
    applied = []

    for rec in all_recommendations:
        if rec["id"] in selected_ids:
            category = rec["category"]
            gain = IMPACT_POINTS.get(rec["impact"], 5)
            simulated_scores[category] = min(100, simulated_scores.get(category, 0) + gain)
            applied.append(rec["title"])

    before = compute_growth_score(category_scores)
    after = compute_growth_score(simulated_scores)

    return {
        "before_score": before["growth_score"],
        "after_score": after["growth_score"],
        "estimated_gain": after["growth_score"] - before["growth_score"],
        "applied_recommendations": applied,
        "new_category_breakdown": after["category_breakdown"],
    }
