#!/usr/bin/env python3
"""Verify the todo-census artifact: re-fetch the live JSONPlaceholder /todos API
and assert the committed CSV/markdown match it, then exit 0 only if everything
is consistent.

Usage: python3 verify
(Single bare command whose exit code is the verdict: 0 = correct, nonzero = wrong.)
"""
import csv
import json
import sys
from urllib.request import urlopen

CSV_PATH = "/root/troupe-standup/wizard-work/ledger/2026-11-01-todo-census/todo-census-2026-11-01.csv"
API_URL = "https://jsonplaceholder.typicode.com/todos"


def fetch(path):
    req = urlopen(path, timeout=30)
    raw = req.read()
    req.close()
    return json.loads(raw.decode("utf-8"))


def read_csv_rows(path):
    with open(path, encoding="utf-8") as f:
        return list(csv.DictReader(f, delimiter="|"))


def main():
    errors = []

    live = fetch(API_URL)
    if not isinstance(live, list):
        print("FAIL: API did not return a list", file=sys.stderr)
        sys.exit(1)

    disk = read_csv_rows(CSV_PATH)

    # 1. Row count
    if len(live) != len(disk):
        errors.append(f"row count mismatch: API={len(live)} CSV={len(disk)}")

    # 2. Completed/pending tallies
    live_done = sum(1 for r in live if r["completed"])
    if live_done != sum(1 for r in disk if r["completed"] == "1"):
        errors.append("completed tally mismatch")

    # 3. Every disk row exists in the live set (id + user_id + title + completed)
    live_map = {(str(r["id"]), str(r["userId"]), r["title"], "1" if r["completed"] else "0"): 1
                for r in live}
    for i, r in enumerate(disk, start=1):
        key = (r["id"], r["user_id"], r["title"], r["completed"])
        if key not in live_map:
            errors.append(f"row {i} not present in live API: {key}")

    # 4. Sampling: first, mid, last ids match the live API
    for idx in (0, len(disk) // 2, len(disk) - 1):
        r = disk[idx]
        live_row = next((x for x in live if x["id"] == int(r["id"])), None)
        if live_row is None or live_row["title"] != r["title"]:
            errors.append(f"sample id={r['id']} mismatch")

    if errors:
        print("VERIFY FAILED")
        for e in errors:
            print(" -", e)
        sys.exit(1)

    print(f"VERIFY OK: {len(disk)} rows match the live API "
          f"(completed={sum(1 for r in disk if r['completed'] == '1')})")
    sys.exit(0)


if __name__ == "__main__":
    main()
