class ScoringEngine:
    def __init__(self, raw_findings: dict):
        self.raw_findings = raw_findings
        self.critical_count = 0
        self.high_count = 0
        self.medium_count = 0
        self.low_count = 0

    def calculate_scores(self) -> dict:
        self._count_severities()

        # Simple mock formula for scoring
        # Start at 100, deduct based on severities
        # critical (-20), high (-10), medium (-5), low (-2)
        score = 100.0 - (
            (self.critical_count * 20.0) +
            (self.high_count * 10.0) +
            (self.medium_count * 5.0) +
            (self.low_count * 2.0)
        )
        score = max(0.0, score)

        if self.critical_count > 0 or score < 50:
            risk_level = "Critical"
        elif self.high_count > 0 or score < 70:
            risk_level = "High"
        elif self.medium_count > 0 or score < 85:
            risk_level = "Medium"
        else:
            risk_level = "Low"

        # Construct specific breakdown scores required in cli_plan.yaml
        sast_risk_score = 100.0 - score
        artifact_risk = 0.0 # Placeholder for advanced AST risk
        dependency_risk = 0.0 # Placeholder
        exploit_risk = 0.0 # Placeholder
        combined_risk = sast_risk_score

        return {
            "vibe_to_value_score": score,
            "risk_level": risk_level,
            "breakdown": {
                "sast_risk_score": sast_risk_score,
                "artifact_risk": artifact_risk,
                "dependency_risk": dependency_risk,
                "exploit_risk": exploit_risk,
                "combined_risk": combined_risk
            },
            "counts": {
                "critical": self.critical_count,
                "high": self.high_count,
                "medium": self.medium_count,
                "low": self.low_count
            }
        }

    def _count_severities(self):
        bandit_results = self.raw_findings.get("bandit", {}).get("results", [])
        for issue in bandit_results:
            severity = str(issue.get("issue_severity", "")).lower()
            if severity == "critical" or severity == "high": # Bandit uses 'HIGH'
                self.high_count += 1
            elif severity == "medium":
                self.medium_count += 1
            elif severity == "low":
                self.low_count += 1
        
        # In custom contexts we might inject critical distinct from high, we distribute them if needed.
        # Bandit only outputs HIGH, MEDIUM, LOW. Let's make a mock rule: 
        # If confidence is HIGH and severity is HIGH -> Critical.
        self.critical_count = 0
        self.high_count = 0
        for issue in bandit_results:
            severity = str(issue.get("issue_severity", "")).upper()
            confidence = str(issue.get("issue_confidence", "")).upper()
            if severity == "HIGH":
                if confidence == "HIGH":
                    self.critical_count += 1
                else:
                    self.high_count += 1
            elif severity == "MEDIUM":
                self.medium_count += 1
            elif severity == "LOW":
                self.low_count += 1
