from math import isfinite
from langchain_typesafe import Choice, Noul, Score, TypeSafeClassifier


def classify_ticket(classifier):
    questions = {
        "route": Choice(instructions="Which team owns this incident?", criteria={
            "technical": "Bugs or integrations", "billing": "Invoices", "human": "Unclear ownership"}),
        "severity": Score(instructions="How blocked are customers?", criteria=[
            "Unaffected", "Workaround available", "Core workflow blocked"]),
        "urgent": Noul(instructions="Does the message describe an ongoing service disruption?"),
    }
    if not questions:
        raise ValueError("At least one question is required")
    response = classifier.invoke({
        "state": {"message": "Checkout fails for every customer.", "scope": "all customers"},
        "questions": questions,
    })
    route = response.choices["route"]
    valid = route.choice in {"technical", "billing", "human"} and isfinite(route.confidence) and 0 <= route.confidence <= 1
    # Teaching threshold only; calibrate with your independently labeled outcomes.
    return {"action": "review" if not valid or route.choice == "human" or route.confidence < 0.75 else "suggest",
            "team": route.choice, "severity": response.scores["severity"].score,
            "urgent_probability": response.nouls["urgent"].noul}


if __name__ == "__main__":
    try:
        print(classify_ticket(TypeSafeClassifier(model="jev-1.13.0", timeout=2)))
    except Exception:
        print({"action": "review", "reason": "provider_or_configuration_failure"})
