#!/usr/bin/env python3
"""Cek jadwal pending TS Law (LI/FB/Threads) dari Repliz API — paginated."""
import sys, datetime
sys.path.insert(0, '/home/mantapman/.hermes/profiles/atun/workspace')
from repliz_client import ReplizClient, ACC

client = ReplizClient()
now = datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.000Z')

ids = {
    'TSLAW_LINKEDIN': ACC.TSLAW_LINKEDIN,
    'TSLAW_FB': ACC.TSLAW_FB,
    'TSLAW_THREADS': ACC.TSLAW_THREADS,
}

# Fetch all schedules (paginated)
all_docs = []
page = 1
while True:
    res = client.get_schedules(page=page, limit=100)
    docs = res.get('docs', [])
    all_docs.extend(docs)
    if page >= res.get('totalPages', 1):
        break
    page += 1

print(f"Total fetched: {len(all_docs)}")

for name, aid in ids.items():
    pend = [s for s in all_docs if s.get('accountId') == aid and s.get('status') == 'pending' and s.get('scheduleAt', '') >= now]
    pend.sort(key=lambda s: s.get('scheduleAt', ''))
    print(f"\n=== {name} ({aid}) — pending mendatang: {len(pend)} ===")
    for s in pend[:50]:
        t = s.get('scheduleAt', '?')
        title = (s.get('title') or '')[:55]
        desc = (s.get('description') or '')[:70].replace('\n', ' ')
        print(f"  {t} | {title} | {desc}")
    if not pend:
        print("  (tidak ada jadwal pending mendatang)")
