#!/usr/bin/env python3
"""Verifikasi INDEPENDEN hc38 (fetch ULANG dari API, tidak percaya output run script)."""
import os, re, sys
sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
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))

IDS = {"threads": "6aaae94bfc107e4fe5a0564f", "linkedin": "6aaae94b9aa606c3bd7cd208",
       "facebook": "6aaae94bfc107e4fe5a05666"}
FILES = ["ss_hc38_website_01_cover.jpg", "ss_hc38_website_02_kesalahan12.jpg",
         "ss_hc38_website_03_kesalahan34.jpg", "ss_hc38_website_04_solusi.jpg",
         "ss_hc38_website_05_cta.jpg"]
CTA = re.compile(r"komentar|pendapat|diskusi|setuju|share|pengalaman|menurut", re.I)

print("\n=== fetch per-ID (GET /public/schedule/<id>) ===")
for name, sid in IDS.items():
    r = c.get("/public/schedule/%s" % sid)
    s = r.get("doc") or r.get("data") or r
    desc = s.get("description") or ""
    med = [os.path.basename((m.get("url") or "").split("?")[0]) for m in (s.get("medias") or [])]
    print("\n[%s] id=%s" % (name, s.get("_id")))
    print("  account=%s type=%s status=%s at=%s" % (s.get("accountId"), s.get("type"), s.get("status"), s.get("scheduleAt")))
    print("  medias=%d %s" % (len(med), med))
    print("  replies=%d" % len(s.get("replies") or []))
    print("  caption %d char | mention=%s | hashtag_kapital=%s | CTA_ekor=%s | link=%s" % (
        len(desc), re.findall(r"@\w+", desc),
        [t for t in re.findall(r"#(\w+)", desc) if t != t.lower()],
        bool(CTA.search(desc[-200:])), "studiosoft.id" in desc))
    print("  title=%s" % (s.get("title") or "")[:70])
    if name == "threads":
        print("  slide urut sesuai: %s" % (med == FILES))
        print("  caption <=500: %s" % (len(desc) <= 500))
        print("  -- caption --\n%s" % desc)

print("\n=== replies Threads 17 Sep 02:0x ===")
for s in sorted([s for s in docs if s.get("accountId") == ACC.STUDIOSOFT_THREADS
                 and (s.get("scheduleAt") or "").startswith("2026-09-17T02:0")],
                key=lambda x: x.get("scheduleAt")):
    print("  %s type=%s status=%s replies=%s" % (s.get("scheduleAt"), s.get("type"), s.get("status"), s.get("replies")))
    print("     %s" % (s.get("description") or "")[:110].replace("\n", " | "))

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

print("\n=== total jadwal pending per akun SS ===")
for name, aid in (("THREADS", ACC.STUDIOSOFT_THREADS), ("LINKEDIN", ACC.STUDIOSOFT_LINKEDIN), ("FB", ACC.STUDIOSOFT_FB)):
    n = len([s for s in docs if s.get("accountId") == aid and (s.get("status") or "").lower() in ("pending", "scheduled", "draft")])
    print("  %s: %d" % (name, n))
