#!/usr/bin/env python3
"""Ringkas status jadwal beberapa akun dari dump Repliz."""
import json
import sys
from collections import Counter

docs = json.load(open(sys.argv[1] if len(sys.argv) > 1 else '/tmp/repliz_all_20260915.json'))
accs = json.load(open('/tmp/repliz_accs_20260915.json'))


def show(aid, label):
    ds = [d for d in docs if d.get('accountId') == aid]
    print(f"\n=== {label} ({len(ds)} jadwal) ===")
    c = Counter((d.get('status'), d.get('type')) for d in ds)
    for k, v in sorted(c.items(), key=lambda x: -x[1]):
        print("  ", k, v)
    errs = Counter((d.get('errorMessage') or '')[:120] for d in ds if d.get('status') == 'error')
    print("  ERROR MSG:")
    for k, v in errs.most_common(6):
        print(f"    {v}x {k}")
    succ = sorted([d for d in ds if d.get('status') == 'success'], key=lambda d: d.get('scheduleAt', ''))
    print("  SUCCESS terakhir:", succ[-1].get('scheduleAt') if succ else '-')
    for s in succ[-3:]:
        print("    -", (s.get('title') or (s.get('description') or '')[:70])[:80])


for aid in sys.argv[2:]:
    show(aid, accs.get(aid, aid))
