#!/usr/bin/env python3
"""Cari bukti: apakah type=album pernah dipakai & sukses di akun FB / Threads?"""
import sys, time, json
from collections import defaultdict
WS = "/home/mantapman/.hermes/profiles/atun/workspace"
sys.path.insert(0, WS)
from repliz_client import get_client

def all_docs(c):
    docs = []
    for p in range(1, 80):
        r = None
        for a in range(8):
            try:
                r = c.get_schedules(page=p, limit=100)
                break
            except Exception as e:
                time.sleep(5 + 3 * a)
        if r is None:
            break
        b = r.get("docs", [])
        if not b:
            break
        docs += b
        if len(b) < 100:
            break
    return docs

c = get_client()
docs = all_docs(c)
json.dump(docs, open("/tmp/repliz_docs.json", "w"))
print("docs:", len(docs))

by_acc = defaultdict(lambda: defaultdict(int))
for d in docs:
    by_acc[(d.get("accountId"), d.get("type"))][d.get("status")] += 1

print("\n-- album per akun (type=album) --")
for (aid, t), st in sorted(by_acc.items()):
    if t == "album":
        print(f"{aid} album: {dict(st)}")

print("\n-- contoh 12 album terbaru (semua akun) --")
alb = sorted([d for d in docs if d.get("type") == "album"], key=lambda d: d.get("scheduleAt") or "", reverse=True)[:12]
for d in alb:
    print(f"  {d.get('accountId')} | {d.get('scheduleAt')} | {d.get('status'):8s} | "
          f"media={len(d.get('medias') or [])} | {len(d.get('description') or '')}c | {d.get('title','')[:40]}")
