#!/usr/bin/env python3
"""Verify hc44: fetch ulang dari API — medias, replies, jadwal anak, jam WIB."""
import json, os, sys
from datetime import datetime, timedelta, timezone

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

c = ReplizClient()
IDS = {
    "threads_album": "6ab2d15228015854006c7001",
    "linkedin": "6ab2d1532cf2c1b59c43a8ea",
    "facebook": "6ab2d1532cf2c1b59c43a8ef",
}


def wib(s):
    if not s:
        return "-"
    try:
        return (datetime.fromisoformat(s.replace("Z", "+00:00")) + timedelta(hours=7)).strftime("%d %b %Y %H:%M WIB")
    except Exception:
        return s


for name, sid in IDS.items():
    try:
        d = c.get("/public/schedule/%s" % sid)
    except Exception as e:
        print("  %s: FETCH GAGAL %s" % (name, e))
        continue
    doc = d.get("doc") or d.get("data") or d
    print("\n### %s (%s)" % (name, sid))
    print("  status=%s type=%s at=%s (%s)" % (doc.get("status"), doc.get("type"),
                                              doc.get("scheduleAt"), wib(doc.get("scheduleAt"))))
    print("  title=%r" % (doc.get("title") or "")[:70])
    for m in (doc.get("medias") or []):
        print("   media: %s" % m.get("url"))
    print("  replies=%s" % (doc.get("replies") or []))
    desc = doc.get("description") or ""
    print("  desc_len=%d tail=%s" % (len(desc), desc[-90:].replace("\n", " | ")))

print("\n### semua jadwal pending yg mereferensikan album hc44 (reply anak):")
docs, page = [], 1
while True:
    d = c.get("/public/schedule?page=%d&limit=100" % page)
    docs.extend(d.get("docs", []))
    if page >= (d.get("totalPages") or 1):
        break
    page += 1
print("  dump %d docs" % len(docs))
for s in docs:
    if "6ab2d15228015854006c7001" in (s.get("replies") or []):
        print("   %s | %s | %s | %s | replies=%s" % (
            s.get("accountId"), s.get("status"), s.get("scheduleAt"), s.get("type"), s.get("replies")))
print("\n### jejak media hc44 di semua jadwal:")
for s in docs:
    for m in (s.get("medias") or []):
        b = os.path.basename((m.get("url") or "").split("?")[0])
        if b.startswith("ss_hc44_gbp_"):
            print("   %s | %s | %s | %s" % (s.get("accountId"), s.get("status"),
                                            s.get("scheduleAt"), b))
            break

st = json.loads(open(WS + "/studiosoft_healthcare_state.json").read())
print("\nstate:", st)
