#!/usr/bin/env python3
"""Analisa dump jadwal: pending per akun Threads >= tanggal tertentu + apakah ada link website sendiri."""
import json, re, sys
from datetime import datetime, timedelta, timezone
from collections import defaultdict

WIB = timezone(timedelta(hours=7))
DUMP = sys.argv[1] if len(sys.argv) > 1 else "/tmp/repliz_all_20260922.json"
SINCE = sys.argv[2] if len(sys.argv) > 2 else "2026-09-22"
docs = json.load(open(DUMP))
ACC = {
    "6a335b9ec5ff4ce3a3225c2a": "studiosoft",
    "6a334e89c5ff4ce3a3225a48": "uky",
    "6a35f8b7c5ff4ce3a322b2ed": "tslaw",
    "6a4bbc5920e4f4f195e52960": "tsc",
    "6a39e461c5ff4ce3a3233e09": "das",
}
OWN = {
    "studiosoft": "studiosoft.id",
    "uky": "studiosoft.id",
    "tslaw": "tslaw.id",
    "tsc": "theskincodeclinic.com",
    "das": "dasindonesia.com",
}

def wib(sa):
    try:
        return datetime.fromisoformat(str(sa).replace("Z", "+00:00")).astimezone(WIB)
    except Exception:
        return None

per = defaultdict(list)
for d in docs:
    if d.get("accountId") not in ACC:
        continue
    t = wib(d.get("scheduleAt"))
    if not t:
        continue
    per[ACC[d["accountId"]]].append((t, d))

for acc in ["tslaw", "tsc", "das", "studiosoft", "uky"]:
    fut = [(t, d) for t, d in per[acc]
           if t.strftime("%Y-%m-%d") >= SINCE and (d.get("status") or "") in ("pending", "draft")]
    fut.sort()
    print(f"\n### {acc}: {len(fut)} pending >= {SINCE}")
    for t, d in fut[:25]:
        desc = d.get("description") or ""
        m = re.search(r"https?://\S+", desc)
        own = "OWNLINK" if m and OWN[acc] in m.group(0) else ("link" if m else "NOLINK")
        print(f"  {t.strftime('%Y-%m-%d %H:%M')} type={str(d.get('type')):6} {own:8} title={(d.get('title') or '')[:34]!r} {m.group(0)[:65] if m else ''}")
