#!/usr/bin/env python3
"""List jadwal TS Law (IG / Threads / FB) yang masih pending dari hari ini ke depan."""
import sys, time
from datetime import datetime, timezone, timedelta
WS = "/home/mantapman/.hermes/profiles/atun/workspace"
sys.path.insert(0, WS)
from repliz_client import get_client

WIB = timezone(timedelta(hours=7))
ACC = {
    "IG": "6a76a54d62caae1e04f30968",
    "THREADS": "6a35f8b7c5ff4ce3a322b2ed",
    "FB": "6a3b41d1c5ff4ce3a32753e8",
}

def all_docs(c):
    docs = []
    for p in range(1, 80):
        r = None
        for a in range(8):
            try:
                r = c.get_schedules(page=p, limit=100)
                break
            except Exception as e:
                print("   retry:", e)
                time.sleep(5 + 3 * a)
        if r is None:
            break
        b = r.get("docs", [])
        if not b:
            break
        docs += b
        if len(b) < 100:
            break
    return docs

c = get_client()
docs = all_docs(c)
print("total docs:", len(docs))
now = datetime.now(timezone.utc)
for label, aid in ACC.items():
    rows = [d for d in docs if d.get("accountId") == aid and d.get("status") == "pending"]
    rows.sort(key=lambda d: d.get("scheduleAt") or "")
    fut = [d for d in rows if (d.get("scheduleAt") or "") >= "2026-09-23T00"]
    print(f"\n=== {label} pending={len(rows)} (mulai 23 Sep: {len(fut)}) ===")
    for d in fut:
        at = datetime.fromisoformat(d["scheduleAt"].replace("Z", "+00:00")).astimezone(WIB)
        med = [(m or {}).get("url", "").split("/")[-1] for m in (d.get("medias") or [])]
        print(f"  {at.strftime('%Y-%m-%d %H:%M')} WIB | {d.get('type'):7s} | {d.get('title','')[:44]:44s} | "
              f"{len(d.get('description') or '')}c | {med}")
