#!/usr/bin/env python3
"""Verifikasi independen hc37 (fetch ULANG dari API)."""
import os, re, sys
sys.path.insert(0, "/home/mantapman/.openclaw/workspace-atun")
from repliz_client import ReplizClient, ACC

c = ReplizClient()
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("total schedules:", len(docs))

FILES = ["ss_hc37_edukasi_01_cover.jpg", "ss_hc37_edukasi_02_alasan.jpg", "ss_hc37_edukasi_03_ide.jpg",
         "ss_hc37_edukasi_04_platform.jpg", "ss_hc37_edukasi_05_cta.jpg"]
CTA = re.compile(r"komentar|pendapat|diskusi|setuju|share|pengalaman|menurut", re.I)

hits = [s for s in docs if any(os.path.basename((m.get("url") or "").split("?")[0]) in FILES
                               for m in (s.get("medias") or []))]
print("\nJadwal dgn media hc37: %d" % len(hits))
for s in hits:
    desc = s.get("description") or ""
    med = [os.path.basename((m.get("url") or "").split("?")[0]) for m in (s.get("medias") or [])]
    print("  acc=%s status=%s type=%s at=%s" % (s.get("accountId"), s.get("status"), s.get("type"), s.get("scheduleAt")))
    print("    slide: %s" % med)
    print("    caption %d char | replies=%d | mention=%s | hashtag_kapital=%s | CTA_ekor=%s" % (
        len(desc), len(s.get("replies") or []), re.findall(r"@\w+", desc),
        [t for t in re.findall(r"#(\w+)", desc) if t != t.lower()], bool(CTA.search(desc[-200:]))))
    print("    link di caption: %s" % ("studiosoft.id" in desc))

# replies milik album Threads
print("\nReplies (Threads, sekitar slot):")
for s in docs:
    if s.get("accountId") == ACC.STUDIOSOFT_THREADS and (s.get("scheduleAt") or "").startswith("2026-09-16T02:0"):
        print("  %s type=%s status=%s replies=%s" % (s.get("scheduleAt"), s.get("type"), s.get("status"), s.get("replies")))
        print("     desc: %s" % (s.get("description") or "")[:120].replace("\n", " | "))

# LinkedIn verify
print("\nLinkedIn hc37:")
for s in docs:
    if s.get("accountId") == ACC.STUDIOSOFT_LINKEDIN and (s.get("scheduleAt") or "").startswith("2026-10-09T10"):
        desc = s.get("description") or ""
        print("  id=%s type=%s medias=%d replies=%d status=%s char=%d link=%s mention=%s CTA=%s" % (
            s.get("_id"), s.get("type"), len(s.get("medias") or []), len(s.get("replies") or []),
            s.get("status"), len(desc), "konten-edukasi-kesehatan-kepercayaan-pasien" in desc,
            re.findall(r"@\w+", desc), bool(CTA.search(desc))))

# FB & IG untouched?
for name, aid in (("FB", ACC.STUDIOSOFT_FB), ("IG", ACC.STUDIOSOFT_IG)):
    pend = [s for s in docs if s.get("accountId") == aid and (s.get("status") or "").lower() in ("pending", "scheduled", "draft")]
    hc37 = [s for s in pend if any(os.path.basename((m.get("url") or "").split("?")[0]) in FILES for m in (s.get("medias") or []))]
    print("\n%s %s: pending=%d | jadwal hc37=%d" % (name, aid, len(pend), len(hc37)))
