#!/usr/bin/env python3
"""Distribusi tanggal jadwal Threads pending yang BELUM punya >=2 replies, per akun."""
import json, datetime, collections
docs = json.load(open('/tmp/tsc_sched_all.json'))
WIB = datetime.timezone(datetime.timedelta(hours=7))
AKUN = {
    "6a334e89c5ff4ce3a3225a48": "uky", "6a335b9ec5ff4ce3a3225c2a": "ss",
    "6a35f8b7c5ff4ce3a322b2ed": "tslaw", "6a39e461c5ff4ce3a3233e09": "das",
    "6a4bbc5920e4f4f195e52960": "tsc", "6a44ed1920e4f4f195d8ecf7": "everybe",
}
now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S")
per_tgl = collections.defaultdict(lambda: 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:
        continue
    if len(d.get("replies") or []) >= 2:
        continue
    t = datetime.datetime.fromisoformat(d["scheduleAt"].replace("Z", "+00:00")).astimezone(WIB)
    per_tgl[t.date()][a] += 1
print("tanggal      " + "".join(f"{a:>9s}" for a in ("das", "everybe", "ss", "tsc", "tslaw", "uky")))
tot = collections.Counter()
for tgl in sorted(per_tgl)[:16]:
    row = per_tgl[tgl]
    for a, n in row.items():
        tot[a] += n
    print(f"{tgl}  " + "".join(f"{row.get(a, 0):9d}" for a in ("das", "everybe", "ss", "tsc", "tslaw", "uky")))
print("\ntotal 16 hari:", dict(tot))
semua = collections.Counter()
for tgl, row in per_tgl.items():
    for a, n in row.items():
        semua[a] += n
print("total semua tanggal:", dict(semua), "| jumlah hari:", len(per_tgl))
