"""Synthetic account-territory reconciliation example. Run with Python 3."""
from collections import Counter
import json

# Supplied business expectations and candidate export; no vendor API is called.
CASES = [
    ("T01", "Geographic match", ["East"], ["East"]),
    ("T02", "Manual named-account exception", ["Strategic"], ["Strategic"]),
    ("T03", "East account sent west", ["East"], ["West"]),
    ("T04", "West account sent east", ["West"], ["East"]),
    ("T05", "Shared coverage lost", ["West", "Specialist"], ["West"]),
    ("T06", "Unapproved specialist coverage", ["East"], ["East", "Specialist"]),
    ("T07", "Approved unassigned review", [], []),
    ("T08", "Shared coverage retained", ["East", "Specialist"], ["East", "Specialist"]),
]

def totals(index):
    return dict(sorted(Counter(t for row in CASES for t in row[index]).items()))

rows = [{"id": ident, "case": label, "expected": expected, "candidate": candidate,
         "missing": sorted(set(expected) - set(candidate)),
         "unexpected": sorted(set(candidate) - set(expected))}
        for ident, label, expected, candidate in CASES]
result = {"accounts": len(rows), "expected_totals": totals(2), "candidate_totals": totals(3),
          "mismatches": [r["id"] for r in rows if r["missing"] or r["unexpected"]], "rows": rows}
assert result["expected_totals"] == result["candidate_totals"]
assert result["mismatches"] == ["T03", "T04", "T05", "T06"]
print(json.dumps(result, indent=2))
