#!/usr/bin/env python3
"""Framing India 2026 (release 1) - sampling reproducibility script.

Reproduces BOTH seeded randomizations from scratch:
  1. Constructed-week day draw from the Jun 18 - Jul 2, 2026 window.
  2. 20% human-validation subsample (115 of 575 story IDs).

Note on seeds: both draws use the literal seed 20260618 (the ISO date of the
window start). They are two INDEPENDENT invocations - the RNG is re-seeded
before each draw, so the second draw does not consume the first draw's stream.
A cleaner design would use two distinct documented seeds; the value is kept
as-is because it is what actually generated the published sample.

Run: python3 sampling.py dataset.json
Prints the day list and the 115 validation IDs; exits nonzero on mismatch
with the shipped selected_days.json / validation_sample.csv if present.
"""
import random, datetime, json, sys, csv, os

SEED = 20260618

# --- Draw 1: constructed week ---
random.seed(SEED)
start, end = datetime.date(2026, 6, 18), datetime.date(2026, 7, 2)
days = [start + datetime.timedelta(days=i) for i in range((end - start).days + 1)]
by_wd = {}
for d in days:
    by_wd.setdefault(d.weekday(), []).append(d)
week = {wd: random.choice(opts) for wd, opts in sorted(by_wd.items())}
selected = sorted(str(d) for d in week.values())
print("Constructed week:", selected)

# --- Draw 2: validation subsample ---
ds = sys.argv[1] if len(sys.argv) > 1 else 'dataset_v2.json'
data = json.load(open(ds))
random.seed(SEED)                      # independent re-seed (see docstring)
ids = sorted(x['id'] for x in data)
sample = sorted(random.sample(ids, round(0.2 * len(ids))))
print(f"Validation sample: {len(sample)} ids; first 10: {sample[:10]}")

# --- Self-check against shipped artifacts, if colocated ---
ok = True
if os.path.exists('selected_days.json'):
    ok &= (json.load(open('selected_days.json')) == selected)
    print("selected_days.json match:", json.load(open('selected_days.json')) == selected)
if os.path.exists('validation_sample.csv'):
    shipped = sorted(int(r['id']) for r in csv.DictReader(open('validation_sample.csv')))
    ok &= (shipped == sample)
    print("validation_sample.csv match:", shipped == sample)
sys.exit(0 if ok else 1)
