#!/usr/bin/env python3
"""Tren harian post Threads per akun + bauran tipe (dari dump penuh)."""
import json, sys, collections, datetime

path = sys.argv[1] if len(sys.argv) > 1 else "/tmp/repliz_full_20261004.json"
docs = json.load(open(path))
ACC = {
    "6a334e89c5ff4ce3a3225a48": "uky_threads",
    "6a335b9ec5ff4ce3a3225c2a": "studiosoft_threads",
    "6a35f8b7c5ff4ce3a322b2ed": "tslaw_threads",
    "6a5f4af462caae1e04e8b9a6": "leit_threads",
    "6a39e461c5ff4ce3a3233e09": "das_threads",
    "6a4bbc5920e4f4f195e52960": "tsc_threads",
    "6a86d84b62caae1e04fff12c": "soto_threads",
    "6a44ed1920e4f4f195d8ecf7": "everybe_threads",
}

def an(doc):
    a = doc.get("account") or {}
    aid = a.get("_id") or doc.get("accountId")
    return ACC.get(aid, str(aid))

hari = collections.defaultdict(lambda: collections.Counter())
which = sys.argv[2] if len(sys.argv) > 2 else "uky_threads"
for doc in docs:
    if an(doc) != which:
        continue
    d = (doc.get("scheduleAt") or "")[:10]
    if not d.startswith("2026-09") and not d.startswith("2026-10"):
        continue
    st = doc.get("status")
    hari[d][st] += 1
    if st == "success":
        hari[d]["ok"] += 1
        hari[d]["t_" + str(doc.get("type"))] += 1

print("===", which)
print(f"{'tanggal':12} {'ok':>3} {'err':>3} {'pend':>4}  tipe")
for d in sorted(hari):
    c = hari[d]
    print(f"{d:12} {c['ok']:>3} {c['error']:>3} {c['pending']:>4}  " +
          " ".join(f"{k}={v}" for k, v in sorted(c.items()) if k.startswith("t_")))
print("TOTAL OK:", sum(c["ok"] for c in hari.values()))
