"""Offline starter scorer. Predictions JSONL: {"id":"case-01","decision":"billing"}.

Synthetic examples are not a representative Persian benchmark. No network calls.
"""
import argparse
import json

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


def keyed(rows):
    result = {}
    for row in rows:
        case_id = row.get("id")
        if not isinstance(case_id, str) or not case_id or case_id in result:
            raise ValueError("Missing or duplicate case ID")
        result[case_id] = row
    return result


def score(cases, predictions):
    cases, predictions = keyed(cases), keyed(predictions)
    if not cases or predictions.keys() - cases.keys():
        raise ValueError("Empty cases or unknown prediction IDs")
    groups = {}
    for case_id, case in cases.items():
        if case.get("gold") not in LABELS or not isinstance(case.get("group"), str):
            raise ValueError("Invalid reference label or group")
        decision = predictions.get(case_id, {}).get("decision")
        valid = isinstance(decision, str) and decision in LABELS
        group = groups.setdefault(case["group"], {"total": 0, "valid": 0, "correct": 0})
        group["total"] += 1
        group["valid"] += int(valid)
        group["correct"] += int(valid and decision == case["gold"])
    total = sum(group["total"] for group in groups.values())
    valid = sum(group["valid"] for group in groups.values())
    correct = sum(group["correct"] for group in groups.values())
    return {"total": total, "valid": valid, "correct": correct,
            "coverage": valid / total, "accuracy_all_cases": correct / total,
            "missing_or_invalid": total - valid, "groups": groups}


def read_jsonl(path):
    with open(path, encoding="utf-8") as source:
        return [json.loads(line) for line in source if line.strip()]


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("cases")
    parser.add_argument("predictions")
    args = parser.parse_args()
    print(json.dumps(score(read_jsonl(args.cases), read_jsonl(args.predictions)), indent=2))
