"""Scoring engine for the guided AISA assessment.

Converts questionnaire responses into the same ScanResult format
used by the code scanner, enabling unified report generation.
"""

from ..scanner.analyzer import ScanResult, LayerResult, CriterionResult
from ..layers import ALL_LAYERS, ALL_CONTRACTS
from .questions import QUESTIONS


# Response values
RESPONSE_YES = "yes"
RESPONSE_PARTIAL = "partial"
RESPONSE_NO = "no"
RESPONSE_NA = "na"

VALID_RESPONSES = {RESPONSE_YES, RESPONSE_PARTIAL, RESPONSE_NO, RESPONSE_NA}


def score_responses(responses: dict[str, str], project_name: str = "Assessment") -> ScanResult:
    """Convert questionnaire responses into a ScanResult.

    Args:
        responses: Dict mapping criterion_id -> response (yes/partial/no/na).
        project_name: Name for the assessment.

    Returns:
        ScanResult compatible with the report generator.
    """
    layer_results = []
    for layer in ALL_LAYERS:
        questions = QUESTIONS.get(layer.id, [])
        criteria_results = []
        for q in questions:
            response = responses.get(q.criterion_id, RESPONSE_NO)
            cr = _response_to_criterion_result(q.criterion_id, q.text, response)
            criteria_results.append(cr)
        layer_results.append(LayerResult(
            id=layer.id,
            name=layer.name,
            criteria_results=criteria_results,
        ))

    contract_results = []
    for contract in ALL_CONTRACTS:
        questions = QUESTIONS.get(contract.id, [])
        criteria_results = []
        for q in questions:
            response = responses.get(q.criterion_id, RESPONSE_NO)
            cr = _response_to_criterion_result(q.criterion_id, q.text, response)
            criteria_results.append(cr)
        contract_results.append(LayerResult(
            id=contract.id,
            name=contract.name,
            criteria_results=criteria_results,
        ))

    return ScanResult(
        project_path=project_name,
        frameworks_detected={},
        layer_results=layer_results,
        contract_results=contract_results,
        files_scanned=0,
        total_lines=0,
    )


def _response_to_criterion_result(criterion_id: str, name: str, response: str) -> CriterionResult:
    """Convert a single response to a CriterionResult."""
    if response == RESPONSE_YES:
        return CriterionResult(criterion_id=criterion_id, criterion_name=name,
                               covered=True, confidence=1.0, evidence=["User confirmed: Yes"])
    elif response == RESPONSE_PARTIAL:
        return CriterionResult(criterion_id=criterion_id, criterion_name=name,
                               covered=True, confidence=0.5, evidence=["User confirmed: Partial"])
    elif response == RESPONSE_NA:
        return CriterionResult(criterion_id=criterion_id, criterion_name=name,
                               covered=True, confidence=1.0, evidence=["Not applicable"])
    else:
        return CriterionResult(criterion_id=criterion_id, criterion_name=name,
                               covered=False, confidence=0.0)
