#!/usr/bin/env python3
"""Siapa pembuat jadwal (createdAt) untuk postingan Threads pending 30 hari ke depan."""
import json, datetime, collections
docs = json.load(open('/tmp/threads_reply_dump.json'))
WIB = datetime.timezone(datetime.timedelta(hours=7))
AKUN = {
    "6a334e89c5ff4ce3a3225a48": "uky", "6a335b9ec5ff4ce3a3225c2a": "ss",
    "6a35f8b7c5ff4ce3a322b2ed": "tslaw", "6a39e461c5ff4ce3a3233e09": "das",
    "6a4bbc5920e4f4f195e52960": "tsc", "6a44ed1920e4f4f195d8ecf7": "everybe",
}


def induk(d):
    for r in d.get("replies") or []:
        if isinstance(r, dict):
            k = [x for x in r.keys() if x.isdigit()]
            if 8 <= len(k) < 40:
                return True
    return False


id_anak = {d.get("_id") for d in docs if induk(d)}
now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S")
c = collections.Counter()
for d in docs:
    a = AKUN.get(d.get("accountId"))
    if not a or d.get("status") != "pending" or (d.get("scheduleAt") or "") <= now or d.get("_id") in id_anak:
        continue
    c[((d.get("createdAt") or "?")[:10], a)] += 1
print("createdAt (UTC) x akun untuk postingan pending ke depan:")
for (tgl, a), n in sorted(c.items()):
    print(f"  {tgl} {a:8s} {n}")
