#!/usr/bin/env python3
"""TSLaw: audit penyelarasan artikel dengan GSC — kueri nyata vs target di Yoast, dan cari peluang."""
import base64, json, re, time, urllib.parse, urllib.request, urllib.error, importlib.util
from datetime import date, timedelta

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
TOKEN = f"{WS}/seo-oauth/google_token_atun.json"
GA4 = "properties/537990618"
SITE_ENC = urllib.parse.quote("https://www.tslaw.id/", safe="")
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"}


def api(u, data=None):
    r = urllib.request.Request(u, headers=H, data=json.dumps(data).encode() if data else None)
    for _ in range(3):
        try:
            with urllib.request.urlopen(r, timeout=150) as x:
                return json.loads(x.read())
        except urllib.error.HTTPError as e:
            return {"err": e.code}
        except Exception:
            time.sleep(3)
    return {}


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)
AUTH = base64.b64encode(f'{m.CFG["user"]}:{m.CFG["pass"]}'.encode()).decode()
UA = {"User-Agent": "Mozilla/5.0"}


def rest(p):
    r = urllib.request.Request(f"https://www.tslaw.id{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 []


akhir = date.today() - timedelta(days=3)
awal = (akhir - timedelta(days=89)).isoformat()
gsc = api(f"https://www.googleapis.com/webmasters/v3/sites/{SITE_ENC}/searchAnalytics/query",
          {"startDate": awal, "endDate": akhir.isoformat(), "dimensions": ["page", "query"], "rowLimit": 5000})
baris = gsc.get("rows", []) if isinstance(gsc, dict) else []

per_halaman = {}
for r in baris:
    u = r["keys"][0].replace("https://www.tslaw.id", "").rstrip("/") or "/"
    d = per_halaman.setdefault(u, {"klik": 0, "tayang": 0, "kueri": []})
    d["klik"] += r["clicks"]; d["tayang"] += r["impressions"]
    d["kueri"].append((r["impressions"], r["clicks"], round(r["position"], 1), r["keys"][1]))

ga = api(f"https://analyticsdata.googleapis.com/v1beta/{GA4}:runReport",
         {"dateRanges": [{"startDate": f"{90}daysAgo", "endDate": "today"}],
          "dimensions": [{"name": "landingPagePlusQueryString"}], "metrics": [{"name": "sessions"}], "limit": 100})
sesi = {r["dimensionValues"][0]["value"].replace("https://www.tslaw.id", "").rstrip("/") or "/": int(r["metricValues"][0]["value"])
        for r in (ga.get("rows", []) if isinstance(ga, dict) else [])}

posts = rest("/wp-json/wp/v2/posts?per_page=100&status=publish&context=edit&_fields=id,slug,link,title,content,meta")
hasil = []
for p in (posts if isinstance(posts, list) else []):
    raw = p["content"]["raw"] or ""
    meta = p.get("meta") or {}
    fokus = meta.get("_yoast_wpseo_focuskw") or ""
    kunci = "/" + p["slug"]
    g = per_halaman.get(kunci, {"klik": 0, "tayang": 0, "kueri": []})
    kueri = sorted(g["kueri"], reverse=True)[:5]
    kat = len(re.sub(r"<[^>]+>", " ", raw).split())
    hasil.append({"id": p["id"], "slug": p["slug"], "kata": kat, "klik": g["klik"], "tayang": g["tayang"],
                  "sesi_ga4": sesi.get(kunci, 0), "fokus": fokus, "kueri": kueri,
                  "fokus_muncul": any(fokus.lower() in k[3].lower() for k in kueri) if fokus else False})

print(f"=== {len(hasil)} artikel | GSC 90 hari ({awal} → {akhir.isoformat()}) ===\n")
n_nol = [x for x in hasil if x["tayang"] == 0]
n_ada_tayang_nol_klik = [x for x in hasil if x["tayang"] > 0 and x["klik"] == 0]
print(f"tanpa tayangan sama sekali : {len(n_nol)}")
print(f"ada tayangan tapi 0 klik   : {len(n_ada_tayang_nol_klik)}")
print(f"fokus tidak cocok kueri    : {len([x for x in hasil if not x['fokus_muncul']])}")

print("\n=== 15 artikel dengan tayangan terbanyak (yang layak dikerjakan dulu) ===")
for x in sorted(hasil, key=lambda z: -z["tayang"])[:15]:
    print(f"   #{x['id']:>5} {x['tayang']:>4} tayang | {x['klik']} klik | sesi GA4 {x['sesi_ga4']:>3} | {x['kata']:>4} kata | fokus: «{x['fokus'][:26]}» {'✅' if x['fokus_muncul'] else '⚠️ tidak di kueri'}")
    for t, k, pos, q in x["kueri"][:2]:
        print(f"        {t:>3} tayang | pos {pos:>5} | {q[:60]}")

print("\n=== artikel tanpa tayangan sama sekali (kandidat digabung/diubah arah) ===")
for x in sorted(n_nol, key=lambda z: z["kata"])[:15]:
    print(f"   #{x['id']:>5} {x['kata']:>4} kata | fokus «{x['fokus'][:34]}» | /{x['slug'][:48]}")

json.dump(hasil, open(f"{WS}/drafts/tslaw_audit_selaras.json", "w"), indent=1, ensure_ascii=False)
print("\n→ drafts/tslaw_audit_selaras.json")
