#!/usr/bin/env python3
"""Lihat slot Threads @ukyhandoko 14 hari ke depan: jam berapa saja yang sudah terisi dan jenisnya."""
import json, re, sys
from collections import Counter, defaultdict
from datetime import datetime, timedelta, timezone

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
sys.path.insert(0, WS)
from repliz_client import get_client, ACC          # noqa: E402

NAMA = {getattr(ACC, n): n for n in dir(ACC) if not n.startswith("_")}
client = get_client()
AKUN = {ACC.UKY_THREADS: "UKY_THREADS", ACC.STUDIOSOFT_THREADS: "STUDIOSOFT_THREADS"}

semua = []
page = 1
while True:
    d = client.get_schedules(page=page, limit=100)
    rows = d.get("docs", []) if isinstance(d, dict) else []
    semua += rows
    if len(rows) < 100:
        break
    page += 1

sekarang = datetime.now(timezone.utc)
batas = sekarang + timedelta(days=15)
peta = defaultdict(list)
for s in semua:
    if s.get("accountId") not in AKUN or s.get("status") != "pending":
        continue
    kapan = str(s.get("scheduleAt") or "")
    if kapan < "2090":
        try:
            t = datetime.fromisoformat(kapan.replace("Z", "+00:00"))
        except Exception:
            continue
        if sekarang <= t <= batas:
            wib = (t + timedelta(hours=7)).strftime("%H:%M")
            hari = (t + timedelta(hours=7)).strftime("%Y-%m-%d")
            peta[AKUN[s["accountId"]]].append((hari, wib, s.get("type"), str(s.get("description") or "")[:40]))
    else:
        peta[AKUN[s["accountId"]]].append(("PARKIR", "-", s.get("type"), str(s.get("description") or "")[:40]))

for akun in sorted(peta):
    print(f"\n=== {akun}: {len(peta[akun])} jadwal (14 hari ke depan + parkir) ===")
    jam = Counter(x[1] for x in peta[akun])
    print("   sebaran jam (WIB):", dict(sorted(jam.items())))
    hari = Counter(x[0] for x in peta[akun])
    print("   per hari:", dict(list(sorted(hari.items()))[:8]))
    print("   contoh beberapa:")
    for x in sorted(peta[akun])[:8]:
        print(f"      {x[0]} {x[1]} {str(x[2]):<6} | {x[3]}")

print("\n=== ringkas ===")
for akun in sorted(peta):
    n_link = len([x for x in peta[akun] if "http" in x[3] or "www." in x[3]])
    print(f"   {akun}: {len(peta[akun])} jadwal | yang memuat tautan: {n_link}")
