#!/usr/bin/env python3
"""Contohkan caption Threads pending (semua akun type=threads) untuk cek nada."""
import random
import sys
from datetime import datetime, timedelta, timezone

sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import get_client  # noqa: E402

WIB = timezone(timedelta(hours=7))
c = get_client()
accs = []
page = 1
while True:
    r = c.get("/public/account?page=%d&limit=100" % page)
    d = r.get("docs") or []
    accs.extend(d)
    if not d or len(accs) >= (r.get("totalDocs") or 0):
        break
    page += 1
tid = {a["_id"]: a.get("name") for a in accs if a.get("type") == "threads"}
print("akun threads: %s" % list(tid.values()))

docs, page = [], 1
while True:
    r = c.get_schedules(page=page, limit=100)
    d = r.get("docs") or r.get("data") or []
    if not d:
        break
    docs.extend(d)
    if len(d) < 100:
        break
    page += 1

pend = [x for x in docs if (x.get("status") or "").lower() == "pending" and x.get("accountId") in tid
        and x.get("type") == "text"]
print("jadwal threads pending (text): %d" % len(pend))
random.seed(11)
for s in random.sample(pend, min(5, len(pend))):
    dt = datetime.strptime((s.get("scheduleAt") or "")[:19], "%Y-%m-%dT%H:%M:%S").replace(
        tzinfo=timezone.utc).astimezone(WIB)
    print("\n--- %s | %s WIB | %d char ---" % (tid.get(s.get("accountId")),
                                               dt.strftime("%d %b %H:%M"), len(s.get("description") or "")))
    print(s.get("description"))
