#!/usr/bin/env python3
import json, sys
from datetime import datetime, timedelta, timezone
WIB = timezone(timedelta(hours=7))
docs = json.load(open('/tmp/all_schedules.json'))
TH = {"studiosoft": "6a335b9ec5ff4ce3a3225c2a", "uky": "6a334e89c5ff4ce3a3225a48",
      "tslaw": "6a35f8b7c5ff4ce3a322b2ed", "tsc": "6a4bbc5920e4f4f195e52960",
      "das": "6a39e461c5ff4ce3a3233e09"}
inv = {v: k for k, v in TH.items()}
from collections import defaultdict
cnt = defaultdict(lambda: defaultdict(int))
start = sys.argv[1] if len(sys.argv) > 1 else "2026-10-19"
for d in docs:
    a = d.get('accountId')
    if a not in inv:
        continue
    sa = d.get('scheduleAt')
    if not sa:
        continue
    try:
        dt = datetime.fromisoformat(str(sa).replace('Z', '+00:00')).astimezone(WIB)
    except Exception:
        continue
    day = dt.strftime('%Y-%m-%d')
    if day < start:
        continue
    cnt[day][inv[a]] += 1
for day in sorted(cnt):
    print(day, dict(cnt[day]))

target = sys.argv[2] if len(sys.argv) > 2 else None
if target:
    print("\n=== ISI DETAIL", target)
    for d in docs:
        a = d.get('accountId')
        if a not in inv:
            continue
        sa = d.get('scheduleAt')
        if not sa:
            continue
        dt = datetime.fromisoformat(str(sa).replace('Z', '+00:00')).astimezone(WIB)
        if dt.strftime('%Y-%m-%d') != target:
            continue
        print(f"\n--- {inv[a]} {dt:%H:%M} status={d.get('status')} id={d.get('_id')} type={d.get('type')} topic={d.get('topic')!r} title={d.get('title')!r}")
        print((d.get('description') or '')[:700])
