#!/usr/bin/env python3
"""Verifikasi menyeluruh TSLaw setelah perbaikan alamat: semua URL sitemap + halaman berdata GSC."""
import os, re, time, urllib.error, urllib.request
from datetime import date, timedelta
from google.oauth2 import service_account
from googleapiclient.discovery import build

SITUS = "https://www.tslaw.id"
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/124.0"}
SITE = "https://www.tslaw.id/"


def status(url):
    try:
        with urllib.request.urlopen(urllib.request.Request(url, headers=UA), timeout=45) as x:
            return x.status
    except urllib.error.HTTPError as e:
        return e.code
    except Exception as e:
        return f"GAGAL {str(e)[:25]}"


print("=== 1. semua alamat artikel di sitemap ===")
idx = urllib.request.urlopen(urllib.request.Request(f"{SITUS}/sitemap_index.xml", headers=UA), timeout=60).read().decode()
subs = [u for u in re.findall(r"<loc>([^<]+)</loc>", idx) if "post-sitemap" in u]
urls = []
for s in subs:
    t = urllib.request.urlopen(urllib.request.Request(s, headers=UA), timeout=60).read().decode()
    urls += re.findall(r"<loc>([^<]+)</loc>", t)
print(f"   total: {len(urls)} artikel")
baik, rusak = 0, []
for u in urls:
    st = status(u)
    if str(st).startswith("2"):
        baik += 1
    else:
        rusak.append((st, u))
    time.sleep(0.2)
print(f"   balas 200: {baik} · bermasalah: {len(rusak)}")
for st, u in rusak[:10]:
    print(f"      {st} · {u}")

print("\n=== 2. halaman statis ===")
for p in ("", "artikel", "tentang", "layanan", "hubungi", "practice-areas"):
    u = f"{SITUS}/{p}" + ("/" if p else "/")
    print(f"   {status(u)} · /{p}")

print("\n=== 3. halaman TSLaw yang punya data di GSC (30 teratas) ===")
creds = service_account.Credentials.from_service_account_file(
    os.path.expanduser("~/.hermes/gsc_service_account.json"),
    scopes=["https://www.googleapis.com/auth/webmasters.readonly"])
gsc = build("webmasters", "v3", credentials=creds, cache_discovery=False)
AKHIR = date.today() - timedelta(days=2)
rows = gsc.searchanalytics().query(siteUrl=SITE, body={
    "startDate": (AKHIR - timedelta(days=89)).isoformat(), "endDate": AKHIR.isoformat(),
    "dimensions": ["page"], "rowLimit": 30}).execute().get("rows", [])
mati = 0
for r in sorted(rows, key=lambda x: -x["impressions"]):
    u = r["keys"][0]
    st = status(u)
    if not str(st).startswith("2"):
        mati += 1
        print(f"   ⚠ {st} · {int(r['impressions'])} tayang · {u}")
    time.sleep(0.3)
print(f"   halaman berdata yang masih mati: {mati} dari {len(rows)}")
