#!/usr/bin/env python3
"""Verifikasi halaman publik #3313 setelah perbaikan 17 Okt 2026."""
import json, re, time, urllib.request, urllib.error

UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36",
      "Cache-Control": "no-cache"}
MAP = "/home/mantapman/.hermes/profiles/atun/workspace/tslaw_pemulihan/perbaikan_20261017"
URL = "https://www.tslaw.id/klausul-penyelesaian-sengketa-kontrak-umkm/"
H2_BARU = ["Dasar Hukum Klausul Penyelesaian Sengketa di Indonesia",
           "Contoh Rumusan Klausul Bertingkat",
           "Mediasi di Pengadilan dan Aturan yang Berlaku",
           "Arbitrase atau Pengadilan untuk Mitra di Luar Kota",
           "Daftar Periksa Sebelum Menandatangani Kontrak"]


def get(u, n=3000000):
    try:
        r = urllib.request.Request(u, headers=UA)
        with urllib.request.urlopen(r, timeout=90) as x:
            return x.status, x.read(n)
    except urllib.error.HTTPError as e:
        return e.code, b""
    except Exception as e:
        return type(e).__name__, b""


ts = int(time.time())
st, b = get(f"{URL}?nocache={ts}")
html = b.decode("utf-8", "replace")
print(f"HTTP {st} | {len(html)} byte | {URL}?nocache={ts}")
hasil = {"url": URL, "http": st, "ts": ts}
if st != 200:
    json.dump(hasil, open(f"{MAP}/live_3313_verifikasi.json", "w"), ensure_ascii=False, indent=1)
    raise SystemExit(1)

# bagian isi artikel saja
m = re.search(r"<article.*?</article>", html, re.S)
isi = m.group(0) if m else html
isi = re.sub(r"<(script|style)[^>]*>.*?</\1>", " ", isi, flags=re.S | re.I)
teks = re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", isi)).strip()

hasil["kata_halaman"] = len(teks.split())
hasil["h2_tampil"] = [h for h in H2_BARU if h in teks]
print(f"  kata di halaman: {hasil['kata_halaman']}")
print(f"  H2 baru tampil: {len(hasil['h2_tampil'])}/{len(H2_BARU)}")
for h in H2_BARU:
    print(f"    {'OK  ' if h in teks else 'HILANG'} {h}")

hasil["wa_6281909222026"] = "wa.me/6281909222026" in html
print(f"  tombol WA: {hasil['wa_6281909222026']} | jumlah wa.me: {len(re.findall(chr(119)+'a.me/[0-9]+', html))}")
hasil["pilar"] = "/law-firm-surabaya" in html

# cek tautan internal
tautan = sorted(set(re.findall(r'href="(https://(?:www\.)?tslaw\.id/[^"#?]*)"', html)))
mati, ok = [], 0
for u in tautan:
    s, _ = get(u, 60000)
    if s == 200:
        ok += 1
    else:
        mati.append((u, s))
print(f"  tautan internal diperiksa: {len(tautan)} | 200: {ok} | mati: {mati}")
hasil["tautan_diperiksa"] = len(tautan)
hasil["tautan_200"] = ok
hasil["tautan_mati"] = mati
hasil["judul_h1"] = (re.search(r"<h1[^>]*>(.*?)</h1>", html, re.S).group(1).strip() if re.search(r"<h1[^>]*>", html) else "")
json.dump(hasil, open(f"{MAP}/live_3313_verifikasi.json", "w"), ensure_ascii=False, indent=1)
open(f"{MAP}/live_3313_verifikasi.html", "w").write(html)
print("  tersimpan: live_3313_verifikasi.json/.html")
