"""Pick ~40 multilingual sites from the shared crawl (crawl-500-2026-11).

Input: the crawl's local raw/domains.jsonl. A site qualifies when its homepage
carried at least two hreflang annotations. Stratified random draw, seed 20261112.
Output: raw/sample.json (domains stay local, never published).
"""
import json, random, sys, pathlib
CRAWL = pathlib.Path(__file__).resolve().parents[4] / "research/crawl-500-2026-11/raw/domains.jsonl"
OUT = pathlib.Path(__file__).resolve().parents[1] / "raw/sample.json"
QUOTA = {"top10k": 17, "mid": 15, "pl": 10}
pool = {k: [] for k in QUOTA}
for line in open(CRAWL):
    d = json.loads(line)
    if d.get("outcome") != "ok":
        continue
    html = (d["home"].get("html") or {})
    hl = html.get("hreflang") or []
    if len(hl) < 2:
        continue
    subs = [s.get("final_url") or s.get("url") for s in (d.get("subpages") or []) if s.get("status") == 200]
    pool[d["stratum"]].append({"domain": d["domain"], "stratum": d["stratum"], "rank": d["rank"],
                               "home": d["home"].get("final_url"), "subpages": subs})
rng = random.Random(20261112)
sample = []
for k, n in QUOTA.items():
    p = sorted(pool[k], key=lambda x: x["domain"])
    sample += rng.sample(p, min(n, len(p)))
json.dump(sample, open(OUT, "w"), indent=1)
print({k: len(v) for k, v in pool.items()}, "->", len(sample))
