#!/usr/bin/env python3
"""Status index SEMUA artikel LEIT via GSC URL Inspection API."""
import base64, json, os, re, time, urllib.request

SITE = "https://www.leitsurabaya.com"
AUTH = base64.b64encode(b"bambangbot:zibd dLIV xY8x onP9 39qL hiNr").decode()
from google.auth.transport.requests import Request
from google.oauth2 import service_account

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(Request())

posts = []
page = 1
while True:
    r = urllib.request.Request(SITE + f"/wp-json/wp/v2/posts?per_page=100&page={page}&_fields=id,link,date",
                               headers={"Authorization": f"Basic {AUTH}", "User-Agent": "Mozilla/5.0"})
    d = json.loads(urllib.request.urlopen(r, timeout=180).read())
    posts += d
    if len(d) < 100:
        break
    page += 1

hasil = {}
for p in sorted(posts, key=lambda x: x["date"], reverse=True):
    body = json.dumps({"inspectionUrl": p["link"], "siteUrl": SITE + "/"}).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", {})
        hasil[p["id"]] = {"link": p["link"], "date": p["date"][:10],
                          "state": st.get("coverageState"), "crawl": str(st.get("lastCrawlTime"))[:10],
                          "verdict": st.get("verdict")}
    except Exception as e:
        hasil[p["id"]] = {"link": p["link"], "date": p["date"][:10], "state": f"ERROR {str(e)[:80]}", "crawl": "-", "verdict": "-"}
    time.sleep(0.2)

json.dump(hasil, open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "leit_index_status.json"), "w"), indent=1)

from collections import Counter
c = Counter(v["state"] for v in hasil.values())
print("=== STATUS INDEX 41 ARTIKEL LEIT ===")
for k, n in c.most_common():
    print(f"  {n:>3} × {k}")
print("\n--- artikel BELUM dikenal Google ---")
for pid, v in hasil.items():
    if v["state"] and ("unknown" in v["state"].lower() or "not indexed" in v["state"].lower()):
        print(f"  #{pid} {v['date']} {v['link'].replace('https://www.leitsurabaya.com','')} → {v['state']}")
