#!/usr/bin/env python3
"""Dump SEMUA jadwal Repliz ke /tmp/repliz_all_<YYYYMMDD>.json (sekali fetch, dipakai ulang)."""
import json, sys, time
sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import get_client

client = get_client()
all_docs = []
for p in range(1, 250):
    got = None
    for i in range(15):
        try:
            got = client.get_schedules(page=p, limit=100)
            break
        except Exception as e:
            print(f"  retry page {p}: {str(e)[:60]}", file=sys.stderr)
            time.sleep(min(40, 3 + 4 * i))
    if got is None:
        raise RuntimeError(f"page {p} gagal")
    b = got.get("docs", [])
    if not b:
        break
    all_docs.extend(b)
    if len(b) < 100:
        break
out = sys.argv[1] if len(sys.argv) > 1 else "/tmp/repliz_all_dump.json"
with open(out, "w") as f:
    json.dump(all_docs, f, ensure_ascii=False)
print(f"OK {len(all_docs)} jadwal → {out}")
