#!/usr/bin/env python3
"""DAS: cari tahu KENAPA klik turun — bentuk penurunan harian, kueri yang hilang, dan pemeriksaan penyebab."""
import json, re, urllib.parse, urllib.request, urllib.error
from datetime import date, timedelta

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
TOKEN = f"{WS}/seo-oauth/google_token_atun.json"
SITE_ENC = urllib.parse.quote("sc-domain:dasindonesia.com", 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 gsc(dim, a, b, baris=5000):
    r = urllib.request.Request(f"https://www.googleapis.com/webmasters/v3/sites/{SITE_ENC}/searchAnalytics/query",
                               data=json.dumps({"startDate": a, "endDate": b, "dimensions": dim,
                                                "rowLimit": baris}).encode(), headers=H)
    for _ in range(3):
        try:
            with urllib.request.urlopen(r, timeout=180) as x:
                return json.loads(x.read()).get("rows", [])
        except urllib.error.HTTPError as e:
            return []
        except Exception:
            import time; time.sleep(3)
    return []


akhir = date.today() - timedelta(days=3)
awal = akhir - timedelta(days=152)          # ~5 bulan
S = lambda x: x.isoformat()

print(f"=== klik & tayangan harian ({S(awal)} → {S(akhir)}) ===")
harian = gsc(["date"], S(awal), S(akhir), 200)
print("   tanggal    klik  tayangan   (perubahan besar ditandai)")
sebelum = None
for r in harian:
    k, t = r["clicks"], r["impressions"]
    tanda = ""
    if sebelum and sebelum[1] > 0 and t < sebelum[1] * 0.5:
        tanda = "  ← tayangan turun >50% dalam sehari"
    print(f"   {r['keys'][0]}  {k:>4}  {t:>8}{tanda}")
    sebelum = (k, t)

print("\n=== rata-rata per bulan ===")
bulan = {}
for r in harian:
    b = r["keys"][0][:7]
    d = bulan.setdefault(b, [0, 0, 0])
    d[0] += r["clicks"]; d[1] += r["impressions"]; d[2] += 1
for b in sorted(bulan):
    kl, ty, hari = bulan[b]
    print(f"   {b}: {kl:>5} klik · {ty:>8} tayangan (rata-rata {round(ty/hari):>6}/hari)")

print("\n=== 20 kueri dengan tayangan terbesar yang HILANG (dulu ada, sekarang nol) ===")
lama = {r["keys"][0]: r for r in gsc(["query"], S(awal), S(awal + timedelta(days=59)), 2000)}
baru = {r["keys"][0]: r for r in gsc(["query"], S(akhir - timedelta(days=29)), S(akhir), 2000)}
hilang = [(r["impressions"], r["clicks"], r["keys"][0], round(r["position"], 1))
          for k, r in lama.items() if k not in baru and r["impressions"] >= 10]
for ty, kl, k, pos in sorted(hilang, reverse=True)[:20]:
    print(f"   {ty:>5} tayangan · {kl:>3} klik · dulu posisi {pos:>5} · {k[:56]}")

print("\n=== kueri baru yang muncul (tanda perubahan perilaku) ===")
baru_saja = [(r["impressions"], r["keys"][0]) for k, r in baru.items() if k not in lama]
for ty, k in sorted(baru_saja, reverse=True)[:10]:
    print(f"   {ty:>5} tayangan · {k[:60]}")

print("\n=== brand vs non-brand (30 hari terakhir vs 30 hari di awal periode) ===")
BRAND = ("das", "das indonesia", "dasindonesia", "bengkel das")
for label, a, b in (("awal periode", S(awal), S(awal + timedelta(days=29))),
                    ("terakhir", S(akhir - timedelta(days=29)), S(akhir))):
    rows = gsc(["query"], a, b, 2000)
    bb = sum(r["impressions"] for r in rows if any(x in r["keys"][0].lower() for x in BRAND))
    nb = sum(r["impressions"] for r in rows if not any(x in r["keys"][0].lower() for x in BRAND))
    kb = sum(r["clicks"] for r in rows if any(x in r["keys"][0].lower() for x in BRAND))
    kn = sum(r["clicks"] for r in rows if not any(x in r["keys"][0].lower() for x in BRAND))
    print(f"   {label:<14} brand: {bb:>6} tayangan/{kb:>3} klik · non-brand: {nb:>6} tayangan/{kn:>3} klik")
