"""Verify both public archives with their shipped independent verifier.

Python 3.10+, jsonschema 4.26+ (<5). Run beside quickstart.py.
Archives are read in memory; only known filenames are written.
"""
import io
from pathlib import Path
import subprocess
import sys
import tempfile
import urllib.request
import zipfile
from quickstart import RELEASES, load_sample

for kind, (slug, _) in RELEASES.items():
    load_sample(kind)  # Check immutable archive and payload hashes first.
    url = f"https://windwaydata.com/downloads/{slug}/1.0.0/{slug}-1.0.0.zip"
    with urllib.request.urlopen(url, timeout=60) as response:
        payload = response.read()
    import hashlib
    if hashlib.sha256(payload).hexdigest() != RELEASES[kind][1]:
        raise ValueError("Archive changed between reads")
    with tempfile.TemporaryDirectory() as directory:
        root = Path(directory)
        with zipfile.ZipFile(io.BytesIO(payload)) as archive:
            for name in ("SCHEMA.json", "data/train.jsonl", "data/validation.jsonl",
                         "data/test.jsonl", "validator/check.py", "validator/common.py",
                         "validator/verify_sample.py"):
                target = root / name
                target.parent.mkdir(parents=True, exist_ok=True)
                target.write_bytes(archive.read(slug + "/" + name))
        print(kind, flush=True)
        subprocess.run([sys.executable, str(root / "validator/verify_sample.py")],
                       cwd=root, check=True)
