#!/usr/bin/env python3
"""Download and verify a Ground example; Python standard library only, no account.

python3 verify-example.py ecommerce-test-data
python3 verify-example.py rare-event-transactions --base-url http://localhost:3102
"""
import argparse
import csv
import hashlib
import io
import json
from pathlib import Path
from urllib.request import urlopen

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("slug", choices=["ecommerce-test-data", "rare-event-transactions"])
parser.add_argument("--base-url", default="https://grounddata.vercel.app")
parser.add_argument("--output", type=Path)
args = parser.parse_args()
output = args.output or Path(args.slug)
output.mkdir(parents=True, exist_ok=True)
base = args.base_url.rstrip("/") + "/examples/" + args.slug + "/"

def download(name):
    with urlopen(base + name, timeout=30) as response:
        return response.read()

manifest_bytes = download("manifest.json")
manifest = json.loads(manifest_bytes)
if manifest["slug"] != args.slug:
    raise ValueError("Unexpected example in manifest")
files = {}
for entry in manifest["files"]:
    name = entry["file"]
    if name not in {"data.csv", "data.jsonl", "spec.json"}:
        raise ValueError("Unexpected file in manifest")
    data = download(name)
    if len(data) != entry["bytes"] or hashlib.sha256(data).hexdigest() != entry["sha256"]:
        raise ValueError("Checksum or size mismatch: " + name)
    files[name] = data
    (output / name).write_bytes(data)
    print("Verified", name, len(data), "bytes")

records = [json.loads(line) for line in files["data.jsonl"].decode().splitlines()]
csv_records = list(csv.DictReader(io.StringIO(files["data.csv"].decode())))
spec = json.loads(files["spec.json"])
if len(records) != manifest["rows"] or len(csv_records) != manifest["rows"]:
    raise ValueError("Unexpected record count")
if spec["seed"] != manifest["seed"] or spec["rowCount"] != manifest["rows"]:
    raise ValueError("Specification does not match manifest")
identifier = "Order ID" if args.slug == "ecommerce-test-data" else "transaction_id"
if len({row[identifier] for row in records}) != len(records):
    raise ValueError("Duplicate identifier")
if args.slug == "ecommerce-test-data":
    missing = sum(row["Rating"] is None for row in records)
    print("Missing ratings:", missing, "/", len(records))
else:
    for channel in ["card_present", "online"]:
        subset = [row for row in records if row["channel"] == channel]
        positive = sum(row["is_fraud"] is True for row in subset)
        print("Fraud labels:", channel, positive, "/", len(subset))
(output / "manifest.json").write_bytes(manifest_bytes)
print("Verified", len(records), "records. Files saved to", output)
