#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Dump caption + title 18 jadwal carousel TSLaw pending ke /tmp/tslaw_car_caps.json (bahan bangun caption v2)."""
import json
import sys
sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import ReplizClient, ACC

AKUN = {ACC.TSLAW_IG: "IG", ACC.TSLAW_FB: "FB", ACC.TSLAW_THREADS: "Threads"}
PREFIX = ("tslaw_mitos10_", "tslaw_mitosproses_", "tslaw_mitoshak_",
          "tslaw_mitoskerja_", "tslaw_mitosbukti_", "tslaw_mitostanah_")

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

out = {}
for d in rows:
    if d.get("accountId") not in AKUN or d.get("status") != "pending":
        continue
    if str(d.get("type", "")).lower() != "album":
        continue
    m = (d.get("medias") or [{}])[0].get("url", "").split("/")[-1]
    if not m.startswith(PREFIX):
        continue
    seri = m.split("_01")[0].replace("tslaw_", "")
    out.setdefault(seri, {})[AKUN[d["accountId"]]] = dict(
        id=d.get("_id"), title=d.get("title"), scheduleAt=d.get("scheduleAt"),
        topic=d.get("topic"), description=d.get("description"),
        medias=[mm.get("url", "").split("/")[-1] for mm in (d.get("medias") or [])],
        replies=d.get("replies"),
    )

json.dump(out, open("/tmp/tslaw_car_caps.json", "w"), ensure_ascii=False, indent=2)
for seri, ch in out.items():
    print(f"### {seri}: {sorted(ch)}")
    for nama, v in ch.items():
        print(f"  [{nama}] {v['id']} | {v['scheduleAt']} | title={v['title']}")
        print(f"    topic={v['topic']} | media={len(v['medias'])}")
        print("    --- caption ---")
        print("    " + (v["description"] or "").replace("\n", "\n    "))
