"""Synthetic answer-reading lab. Python 3.11+, standard library, no network.

The fixture is written by this site, not an observed Jev response. The lesson is
how to read three different answer types, not how accurately Jev classifies.
"""
import json
from math import isfinite

STATE = {
    "message": "Checkout fails for every customer; invoices are still accessible.",
    "scope": "all customers",
}
QUESTIONS = {
    "route": {"type": "choice", "instructions": "Which team owns this incident?",
              "criteria": {"technical": "Bugs or integrations", "billing": "Invoices", "human": "Unclear ownership"}},
    "severity": {"type": "score", "instructions": "How blocked are customers?",
                 "criteria": ["Unaffected", "Workaround available", "Core workflow blocked"]},
    "urgent": {"type": "noul", "instructions": "Does the message describe an ongoing service disruption?"},
}
FIXTURE = {
    "route": {"type": "choice", "choice": "technical", "confidence": 0.8,
              "probabilities": {"technical": 0.85, "billing": 0.10, "human": 0.05}},
    "severity": {"type": "score", "score": 1.8},
    "urgent": {"type": "noul", "noul": 0.9},
}


def bounded_number(value, low, high):
    return (isinstance(value, (int, float)) and not isinstance(value, bool)
            and isfinite(value) and low <= value <= high)


def interpret(answers):
    route, severity, urgent = (answers[key] for key in ("route", "severity", "urgent"))
    if route.get("type") != "choice" or route.get("choice") not in QUESTIONS["route"]["criteria"]:
        raise ValueError("Unexpected Choice option")
    if not bounded_number(route.get("confidence"), 0, 1):
        raise ValueError("Invalid Choice confidence")
    probabilities = route.get("probabilities", {})
    if (set(probabilities) != set(QUESTIONS["route"]["criteria"])
            or not all(bounded_number(p, 0, 1) for p in probabilities.values())
            or abs(sum(probabilities.values()) - 1) > 1e-6):
        raise ValueError("Invalid Choice distribution")
    if severity.get("type") != "score" or not bounded_number(severity.get("score"), 0, 2):
        raise ValueError("Score must be a position in the declared 0–2 scale")
    if urgent.get("type") != "noul" or not bounded_number(urgent.get("noul"), 0, 1):
        raise ValueError("Noul must be a probability")
    return {
        "fixture_only": True,
        "proposed_team": route["choice"],
        "choice_confidence": route["confidence"],
        "winning_option_probability": probabilities[route["choice"]],
        "severity_position_0_to_2": severity["score"],
        "ongoing_disruption_probability": urgent["noul"],
        "action": "review_before_any_ticket_update",
    }


if __name__ == "__main__":
    print("SYNTHETIC FIXTURE — no API request or model measurement")
    print(json.dumps(interpret(FIXTURE), indent=2))
    print("Score illustration only: 0*0 + 1*0.2 + 2*0.8 = 1.8; not 1.8% or hours.")
    print("One Choice holds mutually exclusive queues; independent Noul answers need not sum to 1.")
