"""Run: python3 eval.py cases.jsonl predictions.jsonl"""
import json
import sys
from pathlib import Path

LABELS = {"billing", "technical", "other"}

def read_jsonl(path):
    rows = [json.loads(line) for line in Path(path).read_text(encoding="utf-8").splitlines() if line.strip()]
    ids = [row["id"] for row in rows]
    if len(ids) != len(set(ids)):
        raise ValueError(f"Duplicate IDs in {path}")
    return rows

def main():
    cases = read_jsonl(sys.argv[1])
    if not cases:
        raise ValueError("Dataset is empty")
    if any(case["expected"] not in LABELS for case in cases):
        raise ValueError("Unknown expected label")
    predictions = {row["id"]: row.get("label") for row in read_jsonl(sys.argv[2])}
    unknown = set(predictions) - {case["id"] for case in cases}
    if unknown:
        raise ValueError(f"Unknown prediction IDs: {unknown}")
    passed = 0
    for case in cases:
        actual = predictions.get(case["id"])
        ok = actual == case["expected"]
        passed += ok
        if not ok:
            print(f"FAIL {case['id']}: expected={case['expected']!r}, got={actual!r}")
    print(f"Accuracy: {passed}/{len(cases)} = {passed / len(cases):.1%}")
    # A strict gate for this tiny learning dataset, not a universal release threshold.
    return 0 if passed == len(cases) else 1

if __name__ == "__main__":
    raise SystemExit(main())
