#!/usr/bin/env python3
"""DAS: analisis FINAL — hanya ekspor Surabaya+Sidoarjo, disilang dengan GSC.
Kelompokkan: (A) halaman 1 tapi nol klik, (B) sudah di halaman 2-3, (C) belum muncul sama sekali."""
import csv, json, os
from google.oauth2 import service_account
from googleapiclient.discovery import build

LOK = "/home/mantapman/.hermes/cache/documents/doc_f4edd9ada499_Keyword Stats 2026-09-29 at 08_35_08.csv"
KEY = os.path.expanduser("~/.hermes/gsc_service_account.json")
WS = "/home/mantapman/.hermes/profiles/atun/workspace"

with open(LOK, encoding="utf-16") as f:
    baris = list(csv.reader(f, delimiter="\t"))
head = baris[2]
kp = {}
for r in baris[3:]:
    if not r or not r[0].strip():
        continue
    d = dict(zip(head, r))
    kw = d["Keyword"].strip().lower()

    def num(v):
        v = (v or "").strip().strip('"').replace(".", "").replace(",", ".")
        try:
            return float(v)
        except Exception:
            return 0.0
    kp[kw] = {"vol": num(d.get("Avg. monthly searches")), "komp": (d.get("Competition") or "").strip(),
              "idx": num(d.get("Competition (indexed value)")),
              "bid_lo": num(d.get("Top of page bid (low range)")), "bid_hi": num(d.get("Top of page bid (high range)")),
              "di_akun": (d.get("In account?") or "").strip(),
              "turun_3bln": (d.get("Perubahan tiga bulan") or "").strip()}

gsc = build("searchconsole", "v1", credentials=service_account.Credentials.from_service_account_file(
    KEY, scopes=["https://www.googleapis.com/auth/webmasters.readonly"]), cache_discovery=False)
periode = {"startDate": "2026-07-01", "endDate": "2026-09-28"}
gq = {r["keys"][0].lower(): r for r in gsc.searchanalytics().query(siteUrl="https://dasindonesia.com/", body={
    **periode, "dimensions": ["query"], "rowLimit": 5000}).execute().get("rows", [])}
peta = {}
for r in gsc.searchanalytics().query(siteUrl="https://dasindonesia.com/", body={
        **periode, "dimensions": ["query", "page"], "rowLimit": 5000}).execute().get("rows", []):
    q = r["keys"][0].lower()
    if q not in peta or r["impressions"] > peta[q]["imp"]:
        peta[q] = {"page": r["keys"][1].replace("https://dasindonesia.com", ""), "imp": r["impressions"]}

data = []
for kw, v in kp.items():
    g = gq.get(kw)
    data.append({"kw": kw, **v, "imp": g["impressions"] if g else 0, "klik": g["clicks"] if g else 0,
                 "pos": round(g["position"], 1) if g else None,
                 "halaman": peta.get(kw, {}).get("page", "")})

A = [d for d in data if d["pos"] and d["pos"] <= 10]
B = [d for d in data if d["pos"] and 10 < d["pos"] <= 30]
C = [d for d in data if d["pos"] is None]
bervolume = [d for d in data if d["vol"] > 0]


def tampil(judul, daftar, kunci):
    print(f"\n=== {judul} ({len(daftar)}) ===")
    print(f"{'vol':>4} {'idx':>4} {'komp':<10} {'imp':>4} {'klik':>4} {'pos':>6}  keyword / halaman")
    for d in sorted(daftar, key=kunci, reverse=True)[:14]:
        print(f"{int(d['vol']):>4} {int(d['idx']):>4} {d['komp']:<10} {d['imp']:>4} {d['klik']:>4} "
              f"{str(d['pos'] or '-'):>6}  {d['kw'][:46]:<48} {d['halaman'][:30]}")


print(f"total keyword ekspor Surabaya+Sidoarjo: {len(kp)} | ada volume: {len(bervolume)}")
tampil("A. SUDAH HALAMAN 1 (posisi <=10)", A, lambda x: x["imp"])
tampil("B. HALAMAN 2-3 (posisi 11-30)", B, lambda x: x["imp"])
tampil("C. BELUM MUNCUL SAMA SEKALI (0 impresi)", C, lambda x: x["vol"])

print("\n=== D. volume terbesar (semua kelompok) ===")
for d in sorted(data, key=lambda x: -x["vol"])[:12]:
    print(f"   {int(d['vol']):>4}/bln | idx {int(d['idx']):>3} | {d['komp']:<10} | pos {str(d['pos'] or '-'):>6} | "
          f"imp {d['imp']:>4} | {d['kw'][:48]}")

print("\n=== E. kompetisi paling rendah + ada volume (idx terkecil) ===")
for d in sorted([x for x in data if x["vol"] > 0], key=lambda x: x["idx"])[:10]:
    print(f"   idx {int(d['idx']):>3} | vol {int(d['vol']):>3} | pos {str(d['pos'] or '-'):>6} | {d['kw'][:50]}")

json.dump({"A": A, "B": B, "C": C}, open(f"{WS}/drafts/das_keyword_lokal_final.json", "w"), indent=1, ensure_ascii=False)
print(f"\n→ drafts/das_keyword_lokal_final.json")
