#!/usr/bin/env python3
"""Survei TSLaw 18 Okt: kata, tombol WA, skema FAQ, tautan pilar, kandidat prioritas."""
import base64, 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"}
OUT = "/home/mantapman/.hermes/profiles/atun/workspace/tslaw_pemulihan/survei_20261018.json"


def rest(path, method="GET", data=None):
    h = {"Authorization": f"Basic {AUTH}", **UA}
    body = None
    if data is not None:
        body = json.dumps(data).encode()
        h["Content-Type"] = "application/json"
    r = urllib.request.Request(f"https://{DOM}{path}", data=body, headers=h, method=method)
    for _ in range(3):
        try:
            with urllib.request.urlopen(r, timeout=120) as x:
                return x.status, json.loads(x.read())
        except urllib.error.HTTPError as e:
            return e.code, e.read().decode()[:300]
        except Exception:
            time.sleep(3)
    return 0, "gagal"


def kata(html):
    t = re.sub(r"<(script|style)[^>]*>.*?</\1>", " ", html, flags=re.S | re.I)
    t = re.sub(r"<[^>]+>", " ", t)
    t = re.sub(r"&[a-z]+;", " ", t)
    return len(re.findall(r"[A-Za-z0-9][A-Za-z0-9'\-]*", t))


posts, off = [], 0
while off < 400:
    st, p = rest(f"/wp-json/wp/v2/posts?status=publish,future,draft&context=edit&per_page=100&offset={off}"
                 "&_fields=id,slug,link,title,status,content,meta")
    if not isinstance(p, list) or not p:
        print("stop", st, off, p if not isinstance(p, list) else "")
        break
    posts += p
    off += 100

hasil = []
for p in posts:
    c = p.get("content", {}).get("raw") or p.get("content", {}).get("rendered") or ""
    m = p.get("meta") or {}
    hasil.append({
        "id": p["id"], "slug": p["slug"], "status": p["status"], "link": p["link"],
        "judul": re.sub(r"<[^>]+>", "", p["title"]["rendered"]),
        "kata": kata(c),
        "wa": bool(re.search(r"wa\.me/\d+", c)),
        "wa_rusak": bool(re.search(r"wa\.me/628(?!1909222026)", c)),
        "faq_skema": "FAQPage" in c,
        "faq_tampil": bool(re.search(r"<h2[^>]*>[^<]*faq|Pertanyaan yang Sering", c, re.I)),
        "pilar": "law-firm-surabaya" in c,
        "em_dash": c.count("\u2014"),
        "fokus": m.get("_yoast_wpseo_focuskw", ""),
        "metadesc": m.get("_yoast_wpseo_metadesc", ""),
    })

json.dump(hasil, open(OUT, "w"), ensure_ascii=False, indent=1)
pub = [h for h in hasil if h["status"] == "publish"]
tipis = sorted([h for h in pub if h["kata"] < 1000], key=lambda x: x["kata"])
print(f"total={len(hasil)} publish={len(pub)} <1000kata={len(tipis)} 600-900={len([h for h in tipis if 600<=h['kata']<=900])}")
print("tanpa WA:", len([h for h in pub if not h["wa"]]), "| WA rusak:", [h["id"] for h in pub if h["wa_rusak"]])
print("FAQ tampil tanpa skema:", [h["id"] for h in pub if h["faq_tampil"] and not h["faq_skema"]])
print("tanpa pilar:", len([h for h in pub if not h["pilar"]]))
print("\n-- 14 paling tipis (publish)")
for h in tipis[:14]:
    print(f"  #{h['id']} {h['kata']}kata wa={int(h['wa'])} pilar={int(h['pilar'])} faq={int(h['faq_skema'])} em={h['em_dash']}  {h['slug'][:62]}")
