#!/usr/bin/env python3
"""Pantau 8 jadwal hasil reschedule 21 Sep: tunggu sampai statusnya final (success/error).

Dipakai untuk verifikasi nyata bahwa konten benar-benar TAYANG, bukan cuma `pending`.
Tulis hasil akhir ke log_reschedule_hasil_21sep.json.
"""
import json
import sys
import time
from datetime import datetime, timedelta, timezone

sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import get_client  # noqa: E402

IDS = {
    "6ab106d902078a414acbe46e": "Kreasi Studiosoft (IG album 17:36)",
    "6ab106e202078a414acbe6ed": "Dojo Ksatria (IG video 17:39)",
    "6ab106eb02078a414acbe8f8": "TSC Threads (image 17:42)",
    "6ab106f43032e3a79cf17dab": "LEIT IG (LEIT 63, 17:45)",
    "6ab106fd3032e3a79cf18046": "LEIT TikTok (LEIT 62, 17:48)",
    "6ab1067502078a414acbc509": "uky Threads (Marathon, 17:54)",
    "6ab1068402078a414acbca43": "TSC LinkedIn (17:57)",
    "6ab106c23032e3a79cf16e22": "uky IG (18:12)",
}
WIB = timezone(timedelta(hours=7))
c = get_client()
deadline = time.time() + 3600
final = {}
while time.time() < deadline:
    done = 0
    for i, label in IDS.items():
        try:
            d = c.get_schedule(i)
            d = d.get("doc") or d
            st = (d.get("status") or "?").lower()
            if st in ("success", "error", "failed"):
                done += 1
                final[i] = {"label": label, "status": st,
                            "err": (d.get("errorMessage") or "")[:160],
                            "postId": d.get("postId") or ""}
        except Exception as e:
            final[i] = {"label": label, "status": "cek-gagal", "err": str(e)[:160]}
    print(f"[{datetime.now(WIB):%H:%M:%S} WIB] final {done}/{len(IDS)}")
    if done >= len(IDS):
        break
    time.sleep(90)

print("\n=== HASIL AKHIR ===")
for i, v in final.items():
    print(f"{v['status']:9} | {v['label']:44} | {i} | {v['err']}")
json.dump(final, open("/home/mantapman/.hermes/profiles/atun/workspace/log_reschedule_hasil_21sep.json", "w"),
          indent=1, ensure_ascii=False)
