#!/usr/bin/env python3
"""Audit antrean carousel feed IG SS 08:30 WIB (pending, hari ini s/d akhir Okt)
+ story mirror 08:40. Deteksi bolong + link mati."""
import json, re, sys
WS = "/home/mantapman/.hermes/profiles/atun/workspace"
sys.path.insert(0, WS)
from repliz_client import get_client
ACC_IG = "6a38ac33c5ff4ce3a3231232"

c = get_client()
docs, page = [], 1
while True:
    r = c.get(f"/public/schedule?page={page}&limit=100")
    d = r.get("docs") or []
    docs += d
    if page >= (r.get("totalPages") or 0) or not d:
        break
    page += 1

alb, sto = {}, {}
for d in docs:
    if ((d.get("account") or {}).get("_id") or d.get("accountId")) != ACC_IG:
        continue
    at = d.get("scheduleAt") or ""
    if not at.startswith("2026-10-") or at < "2026-10-06T19:":
        continue
    key = at[:10]
    hm = at[11:16]
    if d.get("type") == "album" and hm == "01:30":
        alb[key] = d
    if d.get("type") == "story" and hm == "01:40":
        sto[key] = d

print(f"album 08:30 pending 06-31 Okt: {len(alb)}")
print(f"story 08:40 pending: {len(sto)}")
tanggal = [f"2026-10-{d:02d}" for d in range(6, 32)]
bolong = [t for t in tanggal if t not in alb]
print("hari TANPA album:", bolong or "tidak ada")
bolong_s = [t for t in tanggal if t in alb and t not in sto]
print("album TANPA story mirror:", bolong_s or "tidak ada")
print()
links = {}
for t in tanggal:
    d = alb.get(t)
    if not d:
        continue
    desc = d.get("description") or ""
    us = re.findall(r"https?://studiosoft\.id[^\s)]*", desc)
    med = [m.get("url", "").split("/")[-1] for m in (d.get("medias") or [])]
    links[t] = us[0] if us else None
    print(f"{t} | {d.get('status'):8s} | {len(med)} media | {med[0][:34]:34s} | {(us[0] if us else 'TANPA LINK')}")
json.dump([{"tgl": t, "id": alb[t]["_id"], "link": links.get(t)} for t in tanggal if t in alb],
          open("/tmp/antrean_car_ss.json", "w"), indent=1)
