#!/usr/bin/env python3
"""Cek fakta 19 Okt: status SIUP/TDP vs NIB (UU 6/2023 + PP 5/2021), akta pendirian (UU 40/2007),
NPWP 16 digit (UU 7/2021 HPP), sertifikat halal (UU 33/2014)."""
import json, re, urllib.request, urllib.error

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
tok = json.load(open(f"{WS}/.serper_token.json"))
KEY = tok.get("api_key") or tok.get("key") or list(tok.values())[0]
UA = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124 Safari/537.36"}


def cari(q, n=6):
    req = urllib.request.Request("https://google.serper.dev/search",
                                 data=json.dumps({"q": q, "gl": "id", "hl": "id", "num": n}).encode(),
                                 headers={"X-API-KEY": KEY, "Content-Type": "application/json"})
    with urllib.request.urlopen(req, timeout=60) as r:
        return json.load(r)


def teks(u, batas=4000000):
    try:
        with urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=120) as x:
            b = x.read(batas)
    except urllib.error.HTTPError as e:
        return f"HTTP {e.code}"
    except Exception as e:
        return f"ERR {str(e)[:70]}"
    t = b.decode("utf-8", "replace")
    t = re.sub(r"<(script|style)[^>]*>.*?</\1>", " ", t, flags=re.S | re.I)
    t = re.sub(r"<[^>]+>", " ", t)
    t = t.replace("&nbsp;", " ").replace("&amp;", "&").replace("&#8217;", "'").replace("&quot;", '"')
    return re.sub(r"\s+", " ", t).strip()


for q in ["PP 5 tahun 2021 perizinan berusaha berbasis risiko NIB menggantikan SIUP TDP",
          "SIUP dan TDP dihapus diganti NIB UU Cipta Kerja pasal",
          "OSS nomor induk berusaha berlaku sebagai TDP API kepabeanan"]:
    print(f"\n=== {q}")
    for o in cari(q).get("organic", [])[:6]:
        print("  -", o.get("link"))
        print("    ", (o.get("snippet") or "").replace("\n", " ")[:240])

for u in ["https://peraturan.bpk.go.id/Details/161846/pp-no-5-tahun-2021",
          "https://peraturan.bpk.go.id/Details/246523/uu-no-6-tahun-2023",
          "https://peraturan.bpk.go.id/Details/39968/uu-no-40-tahun-2007"]:
    t = teks(u)
    print(f"\n===== {u} :: {len(t)} char")
    print("  awal:", t[:120])
    if len(t) < 1500:
        continue
    for kw in ["Nomor Induk Berusaha", "Surat Izin Usaha Perdagangan", "Tanda Daftar Perusahaan"]:
        for m in list(re.finditer(kw, t))[:2]:
            print(f"  [{kw}] ...", t[max(0, m.start()-180):m.start()+320])
            print()
    for n in [7, 8]:
        m = re.search(rf"Pasal {n}\b", t)
        if m:
            print(f"  UU40/2007 Pasal {n}:", t[m.start():m.start()+420])
