#!/usr/bin/env python3
"""Verifikasi halaman publik #2944 tslaw.id setelah perbaikan 14 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"}
URL = "https://www.tslaw.id/7-langkah-hukum-menagih-utang-piutang-secara-profesional-tanpa-main-hakim-sendiri/"
OUT = "/home/mantapman/.hermes/profiles/atun/workspace/tslaw_pemulihan/perbaikan_20261014"


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


st, html = get(f"{URL}?nocache={int(time.time())}")
open(f"{OUT}/live_2944_verifikasi.html", "w").write(html)
print(f"HTTP {st} | {len(html)} byte")
if st != 200:
    raise SystemExit("halaman tidak 200")

isi = re.sub(r"<script[^>]*>.*?</script>", " ", html, flags=re.S | re.I)

H2_BARU = ["Dasar Hukum Penagihan Utang Piutang", "Alat Bukti yang Menentukan Hasil Penagihan",
           "Kapan Hak Menagih Utang Kedaluwarsa?", "Memilih Jalur Hukum Sesuai Nilai Utang",
           "Batas yang Tidak Boleh Dilanggar Saat Menagih", "Mencegah Piutang Macet Sebelum Terjadi"]
hasil = {"http": st, "h2_baru_tampil": {}, "h2_baru_hilang": []}
for h in H2_BARU:
    ada = h in isi
    hasil["h2_baru_tampil"][h] = ada
    if not ada:
        hasil["h2_baru_hilang"].append(h)
print("H2 baru tampil:", sum(hasil["h2_baru_tampil"].values()), "/", len(H2_BARU))

faq = re.findall(r"<h3[^>]*>([^<]*\?)</h3>", isi)
print(f"FAQ tampil (H3 bertanya): {len(faq)}")
for q in faq:
    print("   -", q.strip())
hasil["faq_tampil"] = len(faq)

skema_ok, item, pesan = None, None, ""
for m in re.finditer(r'<script[^>]+application/ld\+json[^>]*>(\{.*?\})</script>', html, re.S):
    try:
        d = json.loads(m.group(1))
    except Exception as e:
        pesan = f"json tidak valid: {e}"
        continue
    if isinstance(d, dict) and d.get("@type") == "FAQPage":
        skema_ok, item = True, len(d.get("mainEntity", []))
        # setiap pertanyaan skema harus tampil di halaman
        qskema = [q["name"] for q in d["mainEntity"]]
        hasil["skema_semua_tanya_tampil"] = all(q in isi for q in qskema)
        hasil["skema_jawaban_pendek"] = all(
            q["acceptedAnswer"]["text"][:60] in isi for q in d["mainEntity"])
print(f"skema FAQPage: valid={skema_ok} item={item} {pesan}")
hasil["skema_faqpage_valid"] = skema_ok
hasil["item_skema"] = item

# tautan internal: harus 200 dan tidak ada 404
link = sorted(set(re.findall(r'href="(https://www\.tslaw\.id/[^"#?]+)"', html)))
mati, diperiksa = [], 0
for u in link:
    if u.rstrip("/") == URL.rstrip("/"):
        continue
    s, _ = get(u)
    diperiksa += 1
    if s != 200:
        mati.append((u, s))
print(f"tautan internal diperiksa: {diperiksa} | mati: {len(mati)}")
for u, s in mati:
    print(f"   {s} {u}")
hasil["link_internal_diperiksa"] = diperiksa
hasil["link_internal_mati"] = mati

hasil["tombol_wa"] = len(re.findall(r"wa\.me/\d{6,}", html))
hasil["kata_halaman"] = len(re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", isi)).split())
print(f"tombol WA: {hasil['tombol_wa']} | tautan pilar /law-firm-surabaya/: {isi.count('/law-firm-surabaya')}")
json.dump(hasil, open(f"{OUT}/live_2944_verifikasi.json", "w"), ensure_ascii=False, indent=1)
print("\nringkasan:", json.dumps({k: v for k, v in hasil.items() if k != "h2_baru_tampil"}, ensure_ascii=False)[:700])
