#!/usr/bin/env python3
"""TSLaw: periksa kondisi terkini setelah Bos memperbaiki sendiri (baca saja, tidak mengubah apa pun)."""
import base64, html, json, re, urllib.request, urllib.error

DOM = "tslaw.id"
AUTH = base64.b64encode(b"bambangbot:EFKm EYGx VQCP Y9go ncPr uM2Y").decode()
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36"}


def rest(path):
    r = urllib.request.Request(f"https://{DOM}{path}", headers={"Authorization": f"Basic {AUTH}", **UA})
    try:
        with urllib.request.urlopen(r, timeout=120) as x:
            return json.loads(x.read())
    except Exception as e:
        return {"__err": str(e)[:80]}


print("=== konten tersimpan (database) ===")
for pid, nama in ((833, "Beranda"), (10, "Hubungi"), (834, "Tentang"), (12, "Layanan"), (2798, "Artikel")):
    d = rest(f"/wp-json/wp/v2/pages/{pid}?context=edit&_fields=content,modified")
    raw = d.get("content", {}).get("raw", "") if isinstance(d, dict) else ""
    blok = raw.count("<!-- wp:spectra")
    print(f"  #{pid} {nama:<9} {len(raw):>7} char | blok spectra: {blok:>3} | diubah: {str(d.get('modified'))[:19]}")

print("\n=== halaman live ===")
for u in ("/", "/hubungi/", "/tentang/", "/practice-areas/", "/artikel/"):
    r = urllib.request.urlopen(urllib.request.Request(f"https://www.tslaw.id{u}", headers=UA), timeout=90)
    h = r.read().decode("utf-8", "replace")
    t = re.search(r"<title>(.*?)</title>", h, re.S)
    teks = re.sub(r"\s+", " ", html.unescape(re.sub(r"<[^>]+>", " ", re.sub(r"<(script|style)[^>]*>.*?</\1>", " ", h, flags=re.S | re.I))))
    print(f"  {r.status} {u:<18} {len(h)//1024:>4} KB | {len(teks.split()):>5} kata | {t.group(1)[:52] if t else '-'}")

print("\n=== tanda bagian asli beranda (kalau sudah di-restore Bos) ===")
h = urllib.request.urlopen(urllib.request.Request("https://www.tslaw.id/", headers=UA), timeout=120).read().decode("utf-8", "replace")
for tanda in ("Penghargaan", "Testimoni Klien", "Bagaimana Kami Bekerja", "Layanan Hukum",
              "Law Firm & Konsultan Hukum Surabaya", "Firma Hukum Profesional di Surabaya"):
    print(f"   {tanda:<38} {'ADA' if tanda in h else 'tidak ada'}")
print(f"   jumlah <h2>: {len(re.findall(r'<h2', h))} | <h1>: {len(re.findall(r'<h1', h))} | gambar: {len(re.findall(r'<img', h))}")
