#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Probe dump: struktur + jadwal same-day 2026-10-04 per akun Threads + daftar URL terpakai."""
import json, re, sys
from datetime import datetime, timedelta, timezone

WIB = timezone(timedelta(hours=7))
DUMP = sys.argv[1] if len(sys.argv) > 1 else "/tmp/repliz_all_20261004.json"
DATE = sys.argv[2] if len(sys.argv) > 2 else "2026-10-04"

raw = json.load(open(DUMP))
if isinstance(raw, dict):
    print("keys:", list(raw.keys())[:10])
    docs = raw.get("docs") or raw.get("data") or raw.get("schedules") or []
else:
    docs = raw
print("dump:", DUMP, "docs:", len(docs))

sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import ACC

TARGETS = {
    "studiosoft": (ACC.STUDIOSOFT_THREADS, "studiosoft.id"),
    "uky": (ACC.UKY_THREADS, "studiosoft.id"),
    "tslaw": (ACC.TSLAW_THREADS, "tslaw.id"),
    "tsc": (ACC.TSC_THREADS, "theskincodeclinic.com"),
    "das": (ACC.DAS_THREADS, "dasindonesia.com"),
}

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

for name, (aid, dom) in TARGETS.items():
    acc = [d for d in docs if d.get("accountId") == aid]
    same = [d for d in acc if (wib(d.get("scheduleAt")) or None) and wib(d["scheduleAt"]).strftime("%Y-%m-%d") == DATE]
    print(f"\n=== {name} ({aid}) jadwal total={len(acc)} same-day({DATE})={len(same)}")
    for d in sorted(same, key=lambda x: wib(x.get('scheduleAt'))):
        dt = wib(d.get("scheduleAt"))
        desc = (d.get("description") or "")
        hasown = dom in desc.lower()
        print(f"   {dt.strftime('%H:%M')} status={d.get('status')} type={d.get('type')} ownlink={hasown} id={d.get('_id')} desc[:70]={desc[:70]!r}")

# URL terpakai (semua status, semua akun) - untuk anti-redundant
URL_RE = re.compile(r'https?://[^\s"\'\\]+')
used = set()
for d in docs:
    blob = json.dumps(d, ensure_ascii=False)
    for u in URL_RE.findall(blob):
        used.add(u.rstrip('/').lower().replace('https://www.', 'https://'))
print("\nTotal URL unik terpakai (dump):", len(used))
json.dump(sorted(used), open("/tmp/used_urls_20261004.json", "w"), ensure_ascii=False)
