#!/usr/bin/env python3
"""Daftar ringkas jadwal pending tanpa reply berisi, per akun."""
import json, datetime, sys, collections
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",
}
mau = sys.argv[1] if len(sys.argv) > 1 else None
now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S")


def isi_reply(d):
    out = []
    for r in d.get("replies") or []:
        if isinstance(r, dict) and (r.get("description") or "").strip():
            out.append(r["description"])
    return out


rows = collections.defaultdict(list)
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(isi_reply(d)) >= 2:
        continue
    t = datetime.datetime.fromisoformat(d["scheduleAt"].replace("Z", "+00:00")).astimezone(WIB)
    rows[a].append((t, d, len(isi_reply(d))))
for a in sorted(rows):
    if mau and a != mau:
        continue
    print(f"\n===== {a}: {len(rows[a])} jadwal =====")
    for t, d, n in sorted(rows[a]):
        jd = (d.get("title") or "—")[:38]
        cuplik = (d.get("description") or "").replace("\n", " ")[:90]
        print(f"{t.strftime('%m-%d %H:%M')} {d.get('type'):5s} rep={n} {d.get('_id')} | {jd} | {cuplik}")
