#!/usr/bin/env python3
"""Simpan daftar lengkap halaman per nomor WhatsApp TSC + contoh konteks tombolnya."""
import json, re, sys
from collections import defaultdict
from concurrent.futures import ThreadPoolExecutor
import urllib.request

UA = {"User-Agent": "Mozilla/5.0 (compatible; AtunBot/1.0)"}
DOM = "theskincodeclinic.com"
WS = "/home/mantapman/.hermes/profiles/atun/workspace"
POLA = [("wa.me", re.compile(r"wa\.me/([0-9%\s+]{7,20})")),
        ("api.whatsapp", re.compile(r"phone=([0-9%\s+]{7,20})"))]


def normal(n):
    d = re.sub(r"\D", "", n.replace("%20", "").replace("+", "").strip())
    if not d:
        return ""
    if d.startswith("0"):
        d = "62" + d[1:]
    elif d.startswith("8"):
        d = "62" + d
    return d


def ambil(u):
    try:
        t = urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=25).read().decode("utf-8", "replace")
    except Exception as e:
        return u, {}, str(e)[:40]
    hasil = {}
    for jenis, pola in POLA:
        for m in pola.finditer(t):
            nm = normal(m.group(1))
            if len(nm) >= 10:
                hasil.setdefault(nm, []).append((jenis, t[max(0, m.start() - 90):m.end() + 60]))
    return u, hasil, None


sm = urllib.request.urlopen(urllib.request.Request(f"https://www.{DOM}/sitemap_index.xml", headers=UA),
                            timeout=60).read().decode("utf-8", "replace")
sub = re.findall(r"<loc>([^<]+)</loc>", sm)
urls = []
for s in sub:
    try:
        x = urllib.request.urlopen(urllib.request.Request(s, headers=UA), timeout=60).read().decode("utf-8", "replace")
        urls += re.findall(r"<loc>([^<]+)</loc>", x)
    except Exception:
        continue
urls = sorted(set(u for u in urls if u.startswith("http")))
print(f"{len(urls)} URL diperiksa", flush=True)

per_nomor = defaultdict(list)
contoh = {}
with ThreadPoolExecutor(max_workers=10) as ex:
    for i, (u, hasil, err) in enumerate(ex.map(ambil, urls), 1):
        for nm, daftar in (hasil or {}).items():
            per_nomor[nm].append(u)
            if nm not in contoh:
                contoh[nm] = daftar[0]
        if i % 150 == 0:
            print(f"  ...{i}/{len(urls)}", flush=True)

print("\n=== ringkasan ===")
for nm in sorted(per_nomor, key=lambda x: -len(per_nomor[x])):
    print(f"  {nm}: {len(per_nomor[nm])} halaman")

print("\n=== contoh konteks tombol ===")
for nm, (jenis, konteks) in contoh.items():
    print(f"\n  {nm} ({jenis}):")
    print("    " + re.sub(r"\s+", " ", konteks))

json.dump({nm: sorted(set(v)) for nm, v in per_nomor.items()},
          open(f"{WS}/drafts/tsc_nomor_wa_per_halaman.json", "w"), ensure_ascii=False, indent=1)

with open(f"{WS}/drafts/tsc_nomor_wa_per_halaman.md", "w") as f:
    f.write("# TSC — daftar halaman per nomor WhatsApp (7 Okt 2026)\n\n")
    f.write(f"Diperiksa {len(urls)} URL dari sitemap, 0 gagal.\n\n")
    for nm in sorted(per_nomor, key=lambda x: -len(per_nomor[x])):
        hal = sorted(set(per_nomor[nm]))
        f.write(f"## {nm} — {len(hal)} halaman\n\n")
        for u in hal:
            f.write(f"- {u}\n")
        f.write("\n")
print(f"\n→ drafts/tsc_nomor_wa_per_halaman.md dan .json")
