#!/usr/bin/env python3
"""Cek status jadwal akun IG LEIT (@leitsurabaya) di Repliz."""
import sys, time
from repliz_client import ReplizClient, ACC

ACC_ID = ACC.LEIT_IG
c = ReplizClient()

stats = {"success": 0, "pending": 0, "error": 0, "cancelled": 0, "lain": 0}
rows = []
page = 1
total_docs = None
while True:
    r = c.get_schedules(page=page, limit=100)
    docs = r.get("docs") if isinstance(r, dict) else None
    if docs is None:
        print("ERR API:", str(r)[:300]); sys.exit(1)
    total_docs = r.get("totalDocs", total_docs)
    for s in docs:
        if s.get("accountId") != ACC_ID:
            continue
        st = s.get("status", "?")
        if st not in stats:
            stats["lain"] += 1
        else:
            stats[st] += 1
        rows.append({
            "at": s.get("scheduleAt", ""),
            "st": st,
            "type": s.get("type", ""),
            "title": (s.get("title") or s.get("topic") or "")[:60],
            "id": s.get("_id") or s.get("id"),
            "err": (s.get("errorMessage") or "")[:120],
        })
    if page * 100 >= (total_docs or 0):
        break
    page += 1
    time.sleep(0.2)

print(f"TOTAL docs API: {total_docs}")
print("STATS LEIT IG:", stats)
print("TOTAL jadwal LEIT IG:", len(rows))

rows.sort(key=lambda x: x["at"])

errs = [r for r in rows if r["st"] == "error"]
print(f"\n=== ERROR ({len(errs)}) ===")
for r in errs:
    print(f"{r['at']} | {r['type']} | {r['title']} | {r['err']} | {r['id']}")

pend = [r for r in rows if r["st"] == "pending"]
print(f"\n=== PENDING ({len(pend)}) — 15 terdekat ke depan ===")
for r in pend[:15]:
    print(f"{r['at']} | {r['type']} | {r['title']} | {r['id']}")

succ = [r for r in rows if r["st"] == "success"]
print(f"\n=== SUCCESS/TAYANG ({len(succ)}) — 5 terakhir ===")
for r in succ[-5:]:
    print(f"{r['at']} | {r['type']} | {r['title']} | {r['id']}")
