"""Jevland example: run with your own TYPESAFE_API_KEY. Makes one live request."""
import os

from typesafe_sdk import Choice, TypeSafeClient


def decide_route(client, message):
    response = client.system_one(
        model="jev-latest",
        state={"message": message},
        questions={
            "route": Choice(
                instructions="Which help topic best matches the message?",
                criteria={
                    "account": "Sign-in or account access",
                    "export": "Downloading or exporting a file",
                    "other": "Another topic or too little information",
                },
            )
        },
    )
    answer = response.choices["route"]
    # A starting policy for this example; tune on your own labelled cases.
    route = answer.choice if answer.confidence >= 0.8 else "review"
    return route, answer, response.model


if __name__ == "__main__":
    if not os.environ.get("TYPESAFE_API_KEY"):
        raise SystemExit("Set TYPESAFE_API_KEY in this terminal before running.")
    with TypeSafeClient() as client:
        route, answer, model = decide_route(
            client, "Where can I download my project as a PDF?"
        )
    print("Model:", model)
    print("Route:", route)
    print("Choice:", answer.choice)
    print("Confidence:", answer.confidence)
    print("Probabilities:", answer.probabilities)
