#!/usr/bin/env python3
"""Periksa aset OAuth/Ads di workspace profil suwarno (tanpa mencetak rahasia)."""
import glob, json, os, re

W = "/home/mantapman/.hermes/profiles/suwarno/workspace"
print("=== 1. berkas terkait oauth / ads / gbp ===")
for f in sorted(glob.glob(f"{W}/**/*", recursive=True)):
    if not os.path.isfile(f):
        continue
    b = os.path.basename(f).lower()
    if any(k in b for k in ("token", "oauth", "credential", "client_secret", "ads", "google")) or \
       any(k in f.lower() for k in ("seo-oauth", "gbp", "ads")):
        print(f"   {f.replace(W, '.')[:96]:<98} {os.path.getsize(f):>7} byte")

print("\n=== 2. isi teknis token OAuth (rahasia disamarkan) ===")
for f in glob.glob(f"{W}/**/*token*.json", recursive=True) + glob.glob(f"{W}/**/client_secret*.json", recursive=True):
    try:
        d = json.load(open(f))
    except Exception as e:
        print(f"   {f}: gagal dibaca ({str(e)[:40]})"); continue
    print(f"\n   {f.replace(W, '.')}")
    if "installed" in d or "web" in d:
        c = d.get("installed") or d.get("web")
        print(f"      jenis        : OAuth client ({'installed' if 'installed' in d else 'web'})")
        print(f"      client_id    : {str(c.get('client_id'))[:34]}...")
        print(f"      client_secret: {'ada' if c.get('client_secret') else 'tidak ada'}")
        print(f"      redirect_uris: {c.get('redirect_uris')}")
    else:
        print(f"      scopes       : {d.get('scopes')}")
        print(f"      token_uri    : {d.get('token_uri')}")
        print(f"      client_id    : {str(d.get('client_id'))[:34]}...")
        print(f"      refresh_token: {'ada' if d.get('refresh_token') else 'TIDAK ADA'}")
        print(f"      expiry       : {d.get('expiry')}")

print("\n=== 3. penanda Google Ads (developer token / login-customer-id) di seluruh profil ===")
for root in ("/home/mantapman/.hermes/profiles/suwarno", "/home/mantapman/.hermes/profiles/atun"):
    for f in glob.glob(f"{root}/**/*", recursive=True):
        if not os.path.isfile(f) or f.endswith((".pyc", ".log")):
            continue
        try:
            isi = open(f, errors="replace").read()
        except Exception:
            continue
        if re.search(r"developer.?token|login.?customer.?id|adwords|google-ads|client_customer_id", isi, re.I):
            tanda = sorted({k for k in ("developer_token", "developer-token", "login_customer_id", "login-customer-id",
                                        "adwords", "google-ads", "client_customer_id") if k in isi.lower()})
            print(f"   {f.replace(root, '…')[:88]:<90} → {tanda}")

print("\n=== 4. berkas permohonan API yang sudah ada ===")
for f in glob.glob(f"{W}/**/permohonan*.md", recursive=True) + glob.glob(f"{W}/**/*api*.md", recursive=True):
    print(f"   {f.replace(W, '.')} ({os.path.getsize(f)} byte)")
