#!/usr/bin/env python3
"""Scan live Repliz API (all pages, all statuses) utk akun DAS:
cek apakah slug kandidat pernah dipakai (URL muncul di description/meta).
"""
import json, urllib.request, urllib.parse
from repliz_client import HEADERS, BASE

DAS_FB = "6a3b41e1c5ff4ce3a327543e"
DAS_IG_THREADS = "6a39e461c5ff4ce3a3233e09"

PROBES = [
    "kapan-mobil-perlu-engine-overhaul",
    "penyebab-mesin-mobil-overheat",
    "biaya-menunda-overhaul-mesin-vs-perawatan-rutin",
    "perawatan-mobil-premium-das-indonesia",
]

def fetch_all(account_id):
    docs = []
    page = 1
    while True:
        url = f"{BASE}/public/schedule?page={page}&limit=100"
        req = urllib.request.Request(url, headers=HEADERS, method="GET")
        try:
            with urllib.request.urlopen(req, timeout=30) as r:
                data = json.loads(r.read())
        except Exception as e:
            print(f"  !! page {page} err: {e}")
            break
        ds = data.get("docs", [])
        docs += [d for d in ds if d.get("accountId") == account_id]
        if page >= data.get("totalPages", 1):
            break
        page += 1
    return docs

for label, acc in [("FB_DAS", DAS_FB), ("IG/Threads_DAS", DAS_IG_THREADS)]:
    docs = fetch_all(acc)
    print(f"=== {label} ({acc}): {len(docs)} schedules ===")
    # batasi rentang tampil utk konteks
    for pr in PROBES:
        hits = []
        for d in docs:
            blob = json.dumps(d)
            if pr in blob:
                desc = (d.get("description") or "").replace("\n", " ")[:90]
                hits.append((d.get("scheduleAt", ""), d.get("status"), desc))
        if hits:
            print(f"  PROBE {pr}: {len(hits)} hit")
            for h in hits[:6]:
                print("     ", h)
        else:
            print(f"  PROBE {pr}: 0 hit")
    # tanggal mulai
    scheds = sorted(d.get("scheduleAt", "") for d in docs)
    if scheds:
        print(f"  rentang scheduleAt: {scheds[0][:10]} .. {scheds[-1][:10]}")
