#!/usr/bin/env python3
import json
from datetime import datetime, timedelta, timezone

WIB = timezone(timedelta(hours=7))
docs = json.load(open("/tmp/scheds_1011.json"))
IDS = {
    "studiosoft": "6a335b9ec5ff4ce3a3225c2a",
    "uky": "6a334e89c5ff4ce3a3225a48",
    "tslaw": "6a35f8b7c5ff4ce3a322b2ed",
    "tsc": "6a4bbc5920e4f4f195e52960",
    "das": "6a39e461c5ff4ce3a3233e09",
}
now = datetime.now(WIB)

for name, aid in IDS.items():
    rows = []
    for d in docs:
        if d.get("accountId") != aid:
            continue
        sa = d.get("scheduleAt")
        if not sa:
            continue
        try:
            dt = datetime.fromisoformat(str(sa).replace("Z", "+00:00")).astimezone(WIB)
        except Exception:
            continue
        rows.append((dt, d))
    rows.sort()
    fut = [r for r in rows if r[0] >= now]
    print(f"\n===== {name} ({aid}) — total {len(rows)}, future {len(fut)} =====")
    for dt, d in fut[:25]:
        print(f"  {dt.strftime('%Y-%m-%d %H:%M')} | {d.get('type'):6} | {d.get('status'):8} | {d.get('_id')} | topic={d.get('topic')}")
        print("      ", (d.get("description") or "").replace("\n", " ")[:110])
    # yang error
    errs = [(dt, d) for dt, d in rows if d.get("status") == "error"]
    if errs:
        print(f"  -- {len(errs)} error (terakhir 3):")
        for dt, d in errs[-3:]:
            print(f"     {dt.strftime('%Y-%m-%d %H:%M')} {d.get('_id')} err={str(d.get('errorMessage'))[:120]}")
