#!/usr/bin/env python3
"""Periksa keberadaan tombol WhatsApp yang terlihat di beberapa halaman TSC (live)."""
import re, sys, urllib.request

UA = {"User-Agent": "Mozilla/5.0"}
URLS = sys.argv[1:] or [
    "https://www.theskincodeclinic.com/",
    "https://www.theskincodeclinic.com/menjaga-skin-barrier-kulit-sehat/",
    "https://www.theskincodeclinic.com/faq-dokter-kulit-krian/",
    "https://www.theskincodeclinic.com/dokter-kulit-kelamin-sidoarjo/",
]
for u in URLS:
    try:
        t = urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=60).read().decode("utf-8", "replace")
    except Exception as e:
        print(f"GAGAL {u[:60]}: {str(e)[:50]}")
        continue
    print(f"\n=== {u.replace('https://www.theskincodeclinic.com', '') or '/'} ({len(t)} char)")
    tombol = re.findall(r"<a[^>]*?(?:wa\.me|api\.whatsapp)[^>]*>(.*?)</a>", t, re.S)
    hrefs = re.findall(r"<a[^>]*?href=\"([^\"]*(?:wa\.me|api\.whatsapp)[^\"]*)\"", t)
    print(f"   tautan WhatsApp: {len(hrefs)}")
    for h in sorted(set(hrefs)):
        nomor = re.search(r"(\d{9,15})", h.replace("%20", ""))
        rusak = " ⚠ ada spasi ter-encode" if "%20" in h or " " in h else ""
        print(f"     - {h[:78]}{rusak}")
    bersih = [re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", x)).strip()[:40] for x in tombol]
    print(f"   teks tombol yang terlihat: {bersih[:6]}")
    # cek widget mengapung
    for petunjuk in ("chaty", "wa-widget", "whatsapp-button", "floating", "wa_me"):
        if petunjuk in t.lower():
            print(f"   penanda '{petunjuk}': ada")
