#!/usr/bin/env python3
"""TSLaw: pindai SEMUA tempat yang mungkin masih menunjuk /practice-areas/:
isi artikel & halaman, menu, widget, meta (Yoast), sitemap, dan halaman yang dirender."""
import base64, html, json, re, time, 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"}
CARI = "practice-areas"


def rest(path):
    r = urllib.request.Request(f"https://{DOM}{path}", headers={"Authorization": f"Basic {AUTH}", **UA})
    for _ in range(3):
        try:
            with urllib.request.urlopen(r, timeout=150) as x:
                return x.status, json.loads(x.read())
        except urllib.error.HTTPError as e:
            return e.code, e.read().decode()[:120]
        except Exception:
            time.sleep(3)
    return 0, "gagal"


def ambil(u):
    try:
        r = urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=60)
        return r.status, r.read().decode("utf-8", "replace")
    except urllib.error.HTTPError as e:
        return e.code, ""
    except Exception:
        return 0, ""


print("=== 1. isi artikel & halaman (termasuk yang tidak terbit) ===")
temuan = []
for tipe in ("posts", "pages"):
    for status in ("publish", "draft", "future", "private", "trash"):
        page = 1
        while True:
            st, d = rest(f"/wp-json/wp/v2/{tipe}?per_page=100&page={page}&status={status}"
                         f"&context=edit&_fields=id,title,content,meta,link")
            if not isinstance(d, list):
                break
            for p in d:
                isi = json.dumps(p, ensure_ascii=False)
                if CARI in isi:
                    jumlah = isi.count(CARI)
                    tempat = []
                    raw = p.get("content", {}).get("raw", "") if isinstance(p.get("content"), dict) else ""
                    if CARI in raw:
                        tempat.append("isi")
                    if CARI in json.dumps(p.get("meta") or {}):
                        tempat.append("meta")
                    temuan.append((tipe[:-1], p["id"], p.get("title", {}).get("rendered", "")[:40], jumlah, tempat))
            if len(d) < 100:
                break
            page += 1
for t in temuan:
    print(f"   {t[0]:<6} #{t[1]:>5} {t[2]:<42} {t[3]}x di {', '.join(t[4])}")
print(f"   total: {len(temuan)} konten" if temuan else "   tidak ada temuan (bersih)")

print("\n=== 2. menu navigasi ===")
for path in ("/wp-json/wp/v2/menu-items?per_page=100", "/wp-json/wp/v2/menus"):
    st, d = rest(path)
    if isinstance(d, list):
        for m in d:
            judul = html.unescape(str(m.get("title", {}).get("rendered", "")))
            url = m.get("url", "")
            tanda = "  ← masih alamat lama" if CARI in str(m) else ""
            print(f"   {judul[:34]:<36} {url[:52]}{tanda}")
    else:
        print(f"   {path.split('?')[0]}: HTTP {st} ({str(d)[:60]})")

print("\n=== 3. halaman yang dirender (menu, footer, widget) ===")
for u in ("/", "/tentang/", "/layanan/", "/hubungi/", "/artikel/",
          "/pengacara-gugat-cerai-surabaya/", "/pengacara-surat-wasiat/"):
    st, h = ambil(f"https://www.tslaw.id{u}")
    n = h.count(CARI)
    n_layanan = h.count("/layanan/")
    print(f"   {st} | {u:<34} practice-areas: {n}x | /layanan/: {n_layanan}x")
    if n:
        for m in re.finditer(re.escape(CARI), h):
            potong = re.sub(r"\s+", " ", h[max(0, m.start() - 120):m.start() + 60])
            print(f"        … {potong}")

print("\n=== 4. sitemap & robots ===")
for u in ("/sitemap_index.xml", "/page-sitemap.xml", "/post-sitemap.xml", "/robots.txt"):
    st, h = ambil(f"https://www.tslaw.id{u}")
    print(f"   {st} | {u:<22} {CARI}: {h.count(CARI)}x")

print("\n=== 5. apakah alamat lama masih hidup ===")
for u in ("/practice-areas/", "/practice-areas/", "/layanan/"):
    st, h = ambil(f"https://www.tslaw.id{u}")
    t = re.search(r"<title>(.*?)</title>", h, re.S) if h else None
    print(f"   {st} | {u:<18} {t.group(1)[:44] if t else ''}")
