from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Dict, List

from .discovery import DiscoveryResult


@dataclass
class ScoreBreakdown:
    overall: int
    sbom_score: int
    vuln_score: int
    practices_score: int
    recommendations: List[Dict[str, Any]]


def compute_scores(discovery: DiscoveryResult) -> ScoreBreakdown:
    recommendations: list[dict[str, Any]] = []

    # SBOM score (40)
    sbom_score = 0
    if discovery.sbom_found:
        sbom_score += 20
        total_components = sum(f.component_count for f in discovery.sbom_files)
        if total_components > 0:
            sbom_score += 10  # placeholder for coverage
            sbom_score += 10  # placeholder for version completeness
    else:
        recommendations.append(
            {
                "id": "sbom_missing",
                "title": "Generate an SBOM for your product",
                "description": "No SBOM was detected. Generate a CycloneDX or SPDX SBOM from your build pipeline.",
                "category": "sbom",
                "craReference": "Annex I, Section 2 – Vulnerability handling requirements",
            }
        )

    # Vulnerability hygiene score (30) – placeholder mid-score for now
    vuln_score = 15

    # Practices score (30)
    practices_score = 0
    if discovery.has_security_md:
        practices_score += 10
    else:
        recommendations.append(
            {
                "id": "security_md_missing",
                "title": "Add a SECURITY.md file",
                "description": "Document how users and researchers should report vulnerabilities.",
                "category": "practices",
                "craReference": "Annex I, Section 2 – Vulnerability handling and disclosure",
            }
        )

    if discovery.has_dependabot:
        practices_score += 10
    else:
        recommendations.append(
            {
                "id": "dependabot_missing",
                "title": "Enable automated dependency updates",
                "description": "Configure Dependabot or a similar tool to keep dependencies up to date.",
                "category": "practices",
                "craReference": "Annex I, Section 1 – Secure development and maintenance",
            }
        )

    if discovery.has_docs_security_section:
        practices_score += 10
    else:
        recommendations.append(
            {
                "id": "docs_security_section_missing",
                "title": "Document security and vulnerability handling in your docs",
                "description": "Add a section describing how you monitor, remediate, and communicate vulnerabilities.",
                "category": "practices",
                "craReference": "Annex I, Section 2 – Vulnerability handling requirements",
            }
        )

    overall = sbom_score + vuln_score + practices_score

    return ScoreBreakdown(
        overall=overall,
        sbom_score=sbom_score,
        vuln_score=vuln_score,
        practices_score=practices_score,
        recommendations=recommendations,
    )

