# Scale numbers for Act 3 (source S21): counts from the saved export of Damien Charlotin's AI Hallucination Cases
# database. Stdlib only. Run from the repo root:
#   python episodes/2026-11-chatgpt-lawyers/02-research/scale/charlotin-scale.py
# Every number here describes what the database lists, not how often it happens (no denominator exists).
import csv, json, re, statistics, pathlib, collections, datetime

HERE = pathlib.Path(__file__).parent
CSV = HERE.parent / "sources/raw/charlotin-2026-10-14.csv"
EXPORTED = "2026-10-14"
rows = list(csv.DictReader(open(CSV, encoding="utf-8")))
us = [r for r in rows if "USA" in r["State(s)"]]
dated = [r for r in us if r["Date"]]

by_year = collections.Counter(int(r["Date"][:4]) for r in dated)
by_quarter = collections.Counter(f'{r["Date"][:4]} Q{(int(r["Date"][5:7]) - 1) // 3 + 1}' for r in dated)
day_of_year = datetime.date.fromisoformat(EXPORTED).timetuple().tm_yday

def usd(r):
    nums = [float(n.replace(",", "")) for n in re.findall(r"[\d,]+(?:\.\d+)?", r["Monetary Penalty"])] if "USD" in r["Monetary Penalty"] else []
    return max(nums) if nums else None

pen = [(usd(r), r) for r in us if usd(r)]
lawyer_pen = sorted(v for v, r in pen if "Lawyer" in r["Party(ies)"])
top = max(pen, key=lambda x: x[0])
mata = next(r for r in us if r["Case Name"].startswith("Mata v. Avianca"))

out = {
    "source": "S21", "exported": EXPORTED, "file": "sources/raw/charlotin-2026-10-14.csv",
    "rows_total": len(rows), "rows_us": len(us), "rows_us_undated": len(us) - len(dated),
    "us_by_year": dict(sorted(by_year.items())),
    "us_by_quarter": dict(sorted(by_quarter.items())),
    "us_2026_per_day": round(by_year[2026] / day_of_year, 2),
    "us_party": dict(collections.Counter(r["Party(ies)"] for r in us).most_common(3)),
    "us_tool_named_chatgpt": sum(1 for r in us if r["AI Tool"].strip().lower() == "chatgpt"),
    "us_with_usd_penalty": len(pen),
    "us_lawyer_usd_penalty_cases": len(lawyer_pen),
    "us_lawyer_usd_penalty_median": statistics.median(lawyer_pen),
    "us_lawyer_usd_penalty_at_or_below_2500": sum(1 for v in lawyer_pen if v <= 2500),
    "us_largest_usd_penalty": {"usd": top[0], "case": top[1]["Case Name"], "court": top[1]["Court"], "date": top[1]["Date"], "party": top[1]["Party(ies)"]},
    "mata": {"date": mata["Date"], "court": mata["Court"], "penalty": mata["Monetary Penalty"]},
    "us_earliest": sorted((r["Date"], r["Case Name"]) for r in dated)[:2],
    "caveat": "Recent quarters keep growing as the database adds rulings late; the 2026 Q4 figure is partial (export Oct 14).",
}
(HERE / "scale.json").write_text(json.dumps(out, indent=1, ensure_ascii=False) + "\n", encoding="utf-8")
print(json.dumps(out, indent=1, ensure_ascii=False))
