#!/usr/bin/env python3
"""Cocokkan nama media jadwal carousel TS Law vs file di openclaw-img + ukuran/urutan."""
import sys, os
from collections import defaultdict
sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import ReplizClient, ACC

IMG = "/home/mantapman/.hermes/profiles/atun/workspace/openclaw-img"
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

AKUN = {ACC.TSLAW_IG: "IG", ACC.TSLAW_FB: "FB", ACC.TSLAW_THREADS: "Threads"}
grup = defaultdict(list)
for d in rows:
    if d.get("accountId") in AKUN and str(d.get("type", "")).lower() == "album" and d.get("status") == "pending":
        key = (d.get("scheduleAt"), d.get("title"))
        grup[key].append(d)

for (sat, title), ds in sorted(grup.items()):
    print(f"\n### {sat} — {title}  ({len(ds)} kanal: {sorted(AKUN[d['accountId']] for d in ds)})")
    for d in ds:
        names = [m["url"].split("/")[-1] for m in (d.get("medias") or [])]
        hilang = [n for n in names if not os.path.exists(os.path.join(IMG, n))]
        dup = len(names) != len(set(names))
        urut = all(names[i].endswith(f"_{i+1:02d}.jpg") for i in range(len(names)))
        sz = [os.path.getsize(os.path.join(IMG, n)) // 1024 for n in names if os.path.exists(os.path.join(IMG, n))]
        print(f"  {AKUN[d['accountId']]:7} {len(names)} media | urut 01..10={urut} | dup={dup} | "
              f"file hilang={hilang or 'tidak ada'} | ukuran {min(sz) if sz else 0}-{max(sz) if sz else 0} KB")
