#!/usr/bin/env python3
"""Cek jadwal dengan tepat 1 reply (perlu 1 tambahan) + tampilkan isi reply yang sudah ada."""
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")
cnt = collections.Counter()
satu = []
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
    n = len(d.get("replies") or [])
    cnt[(a, n if n < 3 else "3+")] += 1
    if n == 1:
        satu.append((a, d))
print("sebaran jumlah reply pada jadwal pending (per akun):")
for (a, n), c in sorted(cnt.items(), key=lambda x: (str(x[0][1]), x[0][0])):
    print(f"  {a:9s} n={n}: {c}")
print(f"\njadwal pending dengan tepat 1 reply: {len(satu)}")
for a, d in satu[:12]:
    t = datetime.datetime.fromisoformat(d["scheduleAt"].replace("Z", "+00:00")).astimezone(WIB)
    r = (d.get("replies") or [{}])[0].get("description") or ""
    print(f"  {a} {t.strftime('%Y-%m-%d %H:%M')} {d.get('type')} id={d.get('_id')} | reply({len(r)}): {r[:160]}")
