#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Dump detail lengkap jadwal Threads 5 akun untuk satu tanggal (default hari ini WIB)."""
import sys, json
from datetime import datetime, timedelta, timezone

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
sys.path.insert(0, WS)
from repliz_client import get_client, ACC

WIB = timezone(timedelta(hours=7))
TARGET = sys.argv[1] if len(sys.argv) > 1 else datetime.now(WIB).strftime("%Y-%m-%d")

ACC_MAP = {
    "studiosoft": ACC.STUDIOSOFT_THREADS,
    "uky": ACC.UKY_THREADS,
    "tslaw": ACC.TSLAW_THREADS,
    "tsc": ACC.TSC_THREADS,
    "das": ACC.DAS_THREADS,
}

c = get_client()
docs = []
for p in range(1, 150):
    r = c.get_schedules(page=p, limit=100)
    b = r.get("docs", [])
    if not b:
        break
    docs += b
    if len(b) < 100:
        break

name_by_id = {v: k for k, v in ACC_MAP.items()}
for d in docs:
    aid = d.get("accountId")
    if aid not in name_by_id:
        continue
    sa = d.get("scheduleAt")
    if not sa:
        continue
    try:
        dt = datetime.fromisoformat(str(sa).replace("Z", "+00:00")).astimezone(WIB)
    except Exception:
        continue
    if dt.strftime("%Y-%m-%d") != TARGET:
        continue
    print("=" * 70)
    print(f"{name_by_id[aid]} | {dt.strftime('%H:%M')} WIB | status={d.get('status')} | type={d.get('type')}")
    print(f"_id={d.get('_id')}  title={d.get('title')!r}  topic={d.get('topic')!r}")
    print(f"replies={d.get('replies')}  mediaType={d.get('mediaType')}  count={len(d.get('media') or [])}")
    print("--- description ---")
    print(d.get("description") or "")
