"""Compare support routing policies. Replay uses illustrative scores and no network."""
import argparse
import json
import math
import os
import time
from pathlib import Path
from urllib.request import Request, urlopen

QUESTION = ("Is this about an existing charge, invoice or refund? "
            "Exclude questions about prices before purchase.")
TICKETS = Path(__file__).with_name("tickets.json")


def destination(probability, cutoff):
    if type(probability) in (float, int) and cutoff <= probability <= 1:
        return "billing"
    return "review"


def make_scorer(provider):
    if provider == "gemini":
        from google import genai
        client = genai.Client(api_key=os.environ["GEMINI_API_KEY"],
                              http_options={"api_version": "v1beta", "timeout": 10000})

        def score(text):
            result = client.decisions.create(
                model="gemini-decision-1", input=text,
                questions={"billing": {"type": "noul", "instructions": QUESTION}},
            )
            return result.answers["billing"].noul

    elif provider == "openai":
        from openai import OpenAI
        client = OpenAI(timeout=10, max_retries=0)

        def score(text):
            result = client.decisions.create(
                model="gpt-6-luna", input=text,
                questions=[{"type": "predicate", "name": "billing", "instructions": QUESTION}],
            )
            answer = result.answers[0]
            return answer.probability if answer.type == "predicate" else None

    elif provider == "foundry":
        endpoint = os.environ["AZURE_ENDPOINT"].rstrip("/")
        deployment = os.environ["DEPLOYMENT_NAME"]
        key = os.environ["AZURE_API_KEY"]

        def score(text):
            body = {"model": deployment, "state": text, "questions": {
                "billing": {"type": "noul", "instructions": QUESTION}
            }}
            request = Request(endpoint + "/providers/microsoft/v1/systemone",
                              data=json.dumps(body).encode(),
                              headers={"Content-Type": "application/json", "api-key": key})
            with urlopen(request, timeout=10) as response:
                return json.load(response)["answers"]["billing"]["noul"]
    else:
        raise ValueError(f"Unknown provider: {provider}")
    return score


def compare(rows, policy, cutoff, scorer=None):
    routed = wrong = calls = errors = 0
    durations = []
    for row in rows:
        queue = "review"
        if policy != "model" and row["billing_form"]:
            queue = "billing"
        elif policy != "rules":
            calls += 1
            if scorer is None:
                probability = row["example_score"]
            else:
                start = time.perf_counter()
                try:
                    probability = scorer(row["text"])
                except Exception as exc:
                    # A failed call leaves the ticket available for a person.
                    print(f'Ticket {row["id"]}: {type(exc).__name__}')
                    probability = None
                durations.append(1000 * (time.perf_counter() - start))
                if type(probability) not in (float, int) or not 0 <= probability <= 1:
                    errors += 1
            queue = destination(probability, cutoff)
        if queue == "billing":
            routed += 1
            wrong += not row["billing"]
    totals = dict(policy=policy, cutoff=cutoff, score_requests=calls, billing=routed,
                  review=len(rows)-routed, wrong_queue=wrong, unusable_scores=errors)
    if durations:
        totals["request_p95_ms"] = round(sorted(durations)[math.ceil(.95 * len(durations))-1])
    return totals


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--provider", choices=["replay", "openai", "gemini", "foundry"], default="replay")
    parser.add_argument("--policy", choices=["rules", "model", "hybrid"], default="hybrid")
    parser.add_argument("--cutoff", type=float, default=.90)
    args = parser.parse_args()
    if not 0 <= args.cutoff <= 1:
        parser.error("cutoff must be between 0 and 1")
    scorer = None if args.provider == "replay" or args.policy == "rules" else make_scorer(args.provider)
    print(json.dumps(compare(json.loads(TICKETS.read_text()), args.policy, args.cutoff, scorer), indent=2))
