#!/usr/bin/env python3
"""Audit artikel LEIT penyasar query uang: judul, H1, kata, FAQ, internal link, status index."""
import base64, html, json, os, re, urllib.request
from google.oauth2 import service_account
from googleapiclient.discovery import build

SITE = "https://www.leitsurabaya.com"
AUTH = base64.b64encode(b"bambangbot:zibd dLIV xY8x onP9 39qL hiNr").decode()

TARGET = {
    3928: "kursus bahasa inggris surabaya timur",
    3925: "les bahasa inggris surabaya barat",
    3825: "kursus bahasa inggris paling terjangkau surabaya",
    3827: "rekomendasi kursus bahasa inggris terjangkau surabaya",
    3902: "les bahasa inggris terdekat surabaya",
    3903: "tempat les bahasa inggris terdekat surabaya",
    3904: "les bahasa inggris terdekat dari rumah",
}


def api(path):
    r = urllib.request.Request(SITE + path, headers={"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"})
    with urllib.request.urlopen(r, timeout=180) as x:
        return json.loads(x.read())


ids = "&".join(f"include%5B%5D={i}" for i in TARGET)
posts = api(f"/wp-json/wp/v2/posts?per_page=20&context=edit&{ids}&_fields=id,link,slug,title,content,excerpt")
allp = api("/wp-json/wp/v2/posts?per_page=100&_fields=id,link")
linkmap = {p["id"]: p["link"] for p in allp}

# siapa menautkan ke siapa
inlinks = {i: [] for i in TARGET}
for pid, url in linkmap.items():
    if pid in TARGET:
        out = None
    try:
        c = api(f"/wp-json/wp/v2/posts/{pid}?context=edit&_fields=content")["content"]["raw"]
    except Exception:
        continue
    for t in TARGET:
        if linkmap[t] in c:
            inlinks[t].append(pid)

print("=== AUDIT ARTIKEL QUERY UANG ===")
for p in sorted(posts, key=lambda x: x["id"]):
    raw = p["content"]["raw"]
    pid = p["id"]
    text = html.unescape(re.sub(r"<[^>]+>", " ", raw))
    words = len(text.split())
    h1 = re.findall(r"<h1[^>]*>(.*?)</h1>", raw, re.S)
    h2 = [re.sub(r"<[^>]+>", "", h) for h in re.findall(r"<h2[^>]*>(.*?)</h2>", raw, re.S)]
    faq_schema = "FAQPage" in raw
    faq_h2 = any("faq" in h.lower() for h in h2)
    out_links = len(set(re.findall(r'href="https://www\.leitsurabaya\.com/([^"#?]+)', raw)))
    print(f"\n#{pid} {p['link']}")
    print(f"   judul WP : {html.unescape(p['title']['rendered'])[:100]}")
    print(f"   H1 artikel: {[re.sub('<[^>]+>','',x)[:70] for x in h1] or 'TIDAK ADA H1'}")
    print(f"   {words} kata | FAQ page: {'ADA' if faq_schema else 'tidak'} | heading FAQ: {'ada' if faq_h2 else 'tidak'}")
    print(f"   link keluar (internal unik): {out_links} | yang menautkan ke artikel ini: {len(inlinks[pid])} → {inlinks[pid][:8]}")
    print(f"   keyphrase target: {TARGET[pid]}")
    print(f"   H2 pertama: {h2[0][:80] if h2 else '-'}")

print("\n=== URL INSPECTION (status index GSC) ===")
creds = service_account.Credentials.from_service_account_file(os.path.expanduser("~/.hermes/gsc_service_account.json"),
                                                              scopes=["https://www.googleapis.com/auth/webmasters.readonly"])
creds.refresh(__import__("google.auth.transport.requests", fromlist=["Request"]).Request())
for pid in sorted(TARGET):
    body = json.dumps({"inspectionUrl": linkmap[pid], "siteUrl": "https://www.leitsurabaya.com/"}).encode()
    req = urllib.request.Request("https://searchconsole.googleapis.com/v1/urlInspection/index:inspect", data=body,
                                 headers={"Authorization": f"Bearer {creds.token}", "Content-Type": "application/json"})
    try:
        res = json.load(urllib.request.urlopen(req, timeout=60))
        st = res.get("inspectionResult", {}).get("indexStatusResult", {})
        print(f"  #{pid} {st.get('coverageState')} | crawled: {st.get('lastCrawlTime','-')[:10]} | verdict: {st.get('verdict')}")
    except Exception as e:
        print(f"  #{pid} ERROR {str(e)[:120]}")
