"""BestAgent AI support-routing reference benchmark, version 1.0.0."""

import json
import re
from pathlib import Path

CASES = [
    ("b01", "I need an invoice for last month.", "billing"),
    ("b02", "Can you issue a refund for the extra seat?", "billing"),
    ("b03", "I was charged twice for the same plan.", "billing"),
    ("b04", "Please switch us to annual payments.", "billing"),
    ("a01", "Where can I reset my password?", "account"),
    ("a02", "I need to change the email used for login.", "account"),
    ("a03", "The verification code never arrived.", "account"),
    ("a04", "I cannot sign in on my new phone.", "account"),
    ("t01", "The dashboard returns an error each time I open it.", "technical"),
    ("t02", "My reports do not export to CSV anymore.", "technical"),
    ("t03", "The browser freezes when I open settings.", "technical"),
    ("t04", "The API times out when I fetch a report.", "technical"),
    ("o01", "Are you hiring technical writers?", "other"),
    ("o02", "May I quote your public documentation?", "other"),
    ("o03", "Where can I find your accessibility statement?", "other"),
    ("o04", "Can I share feedback about the color scheme?", "other"),
]
KEYWORDS = {
    "billing": {"invoice", "refund", "charge", "charged", "payment", "payments"},
    "account": {"password", "login", "verification", "account"},
    "technical": {"error", "export", "timeout", "freezes", "crash", "bug"},
}


def route(text):
    words = set(re.findall(r"[a-z]+", text.lower()))
    labels = [label for label, keywords in KEYWORDS.items() if words & keywords]
    return labels[0] if len(labels) == 1 else "other"


def benchmark():
    results = [
        {"id": identifier, "ticket": ticket, "expected": expected,
         "always_other": "other", "keyword_rules": route(ticket)}
        for identifier, ticket, expected in CASES
    ]
    return {
        "benchmark": "support-routing",
        "version": "1.0.0",
        "source": "Research Benchmark",
        "fixture": "Hand-written synthetic English support tickets",
        "cases": len(results),
        "correct": {
            baseline: sum(row[baseline] == row["expected"] for row in results)
            for baseline in ("always_other", "keyword_rules")
        },
        "rows": results,
    }


if __name__ == "__main__":
    import sys

    if len(sys.argv) == 2:
        Path(sys.argv[1]).write_text(json.dumps(benchmark(), indent=2, sort_keys=True) + "\n", encoding="utf-8")
    else:
        print(json.dumps(benchmark(), indent=2))
