#!/usr/bin/env python3
"""hc50: cek akhir — state/log + pastikan TIDAK ada jadwal LinkedIn/FB baru hari ini dari run ini."""
import json
import sys
import time

sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import ReplizClient, ACC

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
st = json.load(open(WS + "/studiosoft_healthcare_state.json"))
print("STATE:", st)
print("LOG (3 baris terakhir):")
lines = open(WS + "/studiosoft_healthcare_log.txt").read().strip().split("\n")
for ln in lines[-3:]:
    print("  ", ln[:300])

c = ReplizClient()


def get(ep, tries=5):
    last = None
    for i in range(tries):
        try:
            return c.get(ep)
        except Exception as e:
            last = e
            time.sleep(3 + 3 * i)
    raise last


docs, page = [], 1
while True:
    d = get("/public/schedule?page=%d&limit=100" % page)
    docs.extend(d.get("docs", []))
    if page >= (d.get("totalPages") or 1):
        break
    page += 1
print("\ntotal docs:", len(docs))
print("\nJadwal LI/FB/IG akun SS yg scheduleAt mulai 2026-09-29T02:0 (harus hanya FB hc50):")
for s in docs:
    if s.get("accountId") in (ACC.STUDIOSOFT_LINKEDIN, ACC.STUDIOSOFT_FB, ACC.STUDIOSOFT_IG,
                              ACC.STUDIOSOFT_THREADS):
        at = s.get("scheduleAt") or ""
        if at.startswith("2026-09-29T02:0") or at.startswith("2026-09-29T12:30") or at.startswith("2026-09-30T02:0"):
            print("  %s | %s | %s | %s | %s | %s" % (
                s.get("accountId"), at, s.get("status"), s.get("type"),
                len(s.get("medias") or []), (s.get("description") or "")[:70].replace("\n", " / ")))
