#!/usr/bin/env python3
"""TSLaw: cari SEMUA tempat yang masih memakai /practice-areas — di isi konten, menu, dan tampilan halaman."""
import base64, json, re, time, urllib.request, urllib.error, importlib.util

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
spec = importlib.util.spec_from_file_location("cfg_tslaw", f"{WS}/cfg_tslaw.py")
m = importlib.util.module_from_spec(spec); spec.loader.exec_module(m)
DOM, AUTH = "www.tslaw.id", base64.b64encode(f'{m.CFG["user"]}:{m.CFG["pass"]}'.encode()).decode()
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124.0 Safari/537.36"}
POLA = r"practice-areas"


def rest(p):
    r = urllib.request.Request(f"https://{DOM}{p}", headers={"Authorization": f"Basic {AUTH}", **UA})
    for _ in range(3):
        try:
            with urllib.request.urlopen(r, timeout=180) as x:
                return json.loads(x.read())
        except Exception:
            time.sleep(4)
    return []


print("=== A. di isi ARTIKEL (56 artikel) ===")
posts, page, ketemu_post = [], 1, []
while True:
    d = rest(f"/wp-json/wp/v2/posts?per_page=100&page={page}&status=publish&context=edit&_fields=id,slug,title,content")
    if not isinstance(d, list):
        break
    posts += d
    if len(d) < 100:
        break
    page += 1
for p in posts:
    raw = p.get("content", {}).get("raw", "") or ""
    if re.search(POLA, raw, re.I):
        contoh = re.findall(r'href="([^"]*practice-areas[^"]*)"', raw, re.I)
        ketemu_post.append((p["id"], p["slug"], len(re.findall(POLA, raw, re.I)), contoh[:3]))
print(f"   artikel memuat '/practice-areas': {len(ketemu_post)}")
for i, s, n, c in ketemu_post:
    print(f"      #{i} /{s[:52]:<54} {n}x | {c}")

print("\n=== B. di isi HALAMAN (page) ===")
pages = rest("/wp-json/wp/v2/pages?per_page=50&status=publish&context=edit&_fields=id,slug,title,content")
for p in pages if isinstance(pages, list) else []:
    raw = p.get("content", {}).get("raw", "") or ""
    if re.search(POLA, raw, re.I):
        contoh = re.findall(r'href="([^"]*practice-areas[^"]*)"', raw, re.I)
        print(f"      #{p['id']} /{p['slug']:<20} {len(re.findall(POLA, raw, re.I))}x | {contoh[:3]}")

print("\n=== C. di tampilan halaman (menu, footer, widget) ===")
UJI = ["/", "/layanan/", "/tentang/", "/hubungi/", "/artikel/", "/law-firm-surabaya/",
       "/pengacara-gugat-cerai-surabaya/", "/biaya-jasa-law-firm-surabaya/"]
for u in UJI:
    try:
        h = urllib.request.urlopen(urllib.request.Request(f"https://{DOM}{u}", headers=UA), timeout=120).read().decode("utf-8", "replace")
        n = len(re.findall(POLA, h, re.I))
        if n:
            print(f"\n   {u} → {n}x")
            for mm in re.finditer(r"<a[^>]*practice-areas[^>]*>(.{0,60}?)</a>", h, re.I | re.S):
                tag = mm.group(0)
                isi = re.sub(r"<[^>]+>", "", mm.group(1)).strip()
                kelas = re.search(r'class="([^"]{0,60})"', tag)
                print(f"      «{isi[:40]}» | kelas: {kelas.group(1) if kelas else '-'}")
                print(f"         {tag[:160]}")
        else:
            print(f"   {u} → tidak ada")
    except Exception as e:
        print(f"   {u} → gagal {str(e)[:60]}")

print("\n=== D. di sitemap & robots ===")
for s in ("/sitemap_index.xml", "/page-sitemap.xml", "/post-sitemap.xml", "/robots.txt"):
    try:
        isi = urllib.request.urlopen(urllib.request.Request(f"https://{DOM}{s}", headers=UA), timeout=90).read().decode("utf-8", "replace")
        print(f"   {s:<22} memuat practice-areas: {len(re.findall(POLA, isi, re.I))}x")
    except Exception as e:
        print(f"   {s:<22} gagal {str(e)[:50]}")

print("\n=== E. status halaman & riwayat GSC ===")
import urllib.parse
TOKEN = f"{WS}/seo-oauth/google_token_atun.json"
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
c = Credentials.from_authorized_user_file(TOKEN)
if c.expired and c.refresh_token:
    c.refresh(Request()); open(TOKEN, "w").write(c.to_json())
H = {"Authorization": f"Bearer {c.token}", "Content-Type": "application/json"}
isi = {"startDate": "2026-07-01", "endDate": "2026-09-27", "dimensions": ["page", "query"], "rowLimit": 200,
       "dimensionFilterGroups": [{"filters": [{"dimension": "page", "operator": "contains", "expression": "practice-areas"}]}]}
r = urllib.request.Request(f"https://www.googleapis.com/webmasters/v3/sites/{urllib.parse.quote('https://www.tslaw.id/', safe='')}/searchAnalytics/query",
                           data=json.dumps(isi).encode(), headers=H)
try:
    with urllib.request.urlopen(r, timeout=120) as x:
        d = json.loads(x.read())
    rows = d.get("rows", [])
    print(f"   kueri yang mendarat di /practice-areas/ (Jul–Sep): {len(rows)}")
    for x2 in sorted(rows, key=lambda z: -z["impressions"])[:10]:
        print(f"      {x2['clicks']:>2} klik | {x2['impressions']:>3} tayang | pos {round(x2['position'],1):>5} | {x2['keys'][1][:50]}")
except Exception as e:
    print("   gagal:", str(e)[:100])
