#!/usr/bin/env python3
"""Dump jadwal Threads pending (tanpa >=2 replies) pada rentang tanggal ke berkas teks,
supaya bisa ditulis reply-nya. Pakai: python3 _dump_threads_pending.py 2026-10-16 2026-10-19
"""
import json, datetime, sys
docs = json.load(open('/tmp/tsc_sched_all.json'))
WIB = datetime.timezone(datetime.timedelta(hours=7))
AKUN = {
    "6a334e89c5ff4ce3a3225a48": "uky", "6a335b9ec5ff4ce3a3225c2a": "ss",
    "6a35f8b7c5ff4ce3a322b2ed": "tslaw", "6a39e461c5ff4ce3a3233e09": "das",
    "6a4bbc5920e4f4f195e52960": "tsc", "6a44ed1920e4f4f195d8ecf7": "everybe",
}
awal = datetime.date.fromisoformat(sys.argv[1])
akhir = datetime.date.fromisoformat(sys.argv[2])
now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S")
baris = []
for d in docs:
    a = AKUN.get(d.get("accountId"))
    if not a or d.get("status") != "pending" or (d.get("scheduleAt") or "") <= now:
        continue
    if len(d.get("replies") or []) >= 2:
        continue
    t = datetime.datetime.fromisoformat(d["scheduleAt"].replace("Z", "+00:00")).astimezone(WIB)
    if not (awal <= t.date() <= akhir):
        continue
    baris.append((t, a, d))
baris.sort(key=lambda x: (x[1], x[0]))
print(f"{len(baris)} jadwal {awal}..{akhir}\n")
for t, a, d in baris:
    desc = (d.get("description") or "").replace("\n", " ⏎ ")
    print(f"### {a} | {t.strftime('%Y-%m-%d %H:%M')} | id={d.get('_id')} | type={d.get('type')} | "
          f"media={len(d.get('medias') or [])} | topic={d.get('topic') or '-'}")
    print(f"TITLE: {d.get('title') or '-'}")
    print(f"TEKS: {desc}")
    for m in (d.get("medias") or []):
        print(f"  media: {m.get('url')}")
    print()
