#!/usr/bin/env python3
"""Jadwalkan draf #9609 ke hari kosong pertama (2026-10-27T08:00 waktu situs)."""
import base64, json, re, sys, time, urllib.request

S = "https://studiosoft.id"
PID = 9609
TGL = "2026-10-27T08:00:00"
H = {"Authorization": "Basic " + base64.b64encode(b"bambangbot:q26r RePo BVy6 pwU2 F3z7 ouyl").decode(),
     "User-Agent": "Mozilla/5.0"}
HJ = {**H, "Content-Type": "application/json"}


def rest(p, method="GET", data=None):
    body = json.dumps(data).encode() if data is not None else None
    r = urllib.request.Request(S + p, data=body, headers=HJ, method=method)
    try:
        with urllib.request.urlopen(r, timeout=180) as x:
            return json.loads(x.read())
    except Exception as e:
        return {"err": str(e)[:120]}


tz = rest("/wp-json/")
print("zona situs:", tz.get("timezone_string"), "| gmt_offset:", tz.get("gmt_offset"))

sebelum = rest(f"/wp-json/wp/v2/posts/{PID}?context=edit&_fields=status,slug,date,content,featured_media")
isi_sebelum = (sebelum.get("content") or {}).get("raw", "")
print(f"sebelum: status={sebelum.get('status')} slug={sebelum.get('slug')} media={sebelum.get('featured_media')} "
      f"isi={len(isi_sebelum)} char")

if "--go" not in sys.argv:
    print("\n(uji, pakai --go untuk menjadwalkan)")
    sys.exit(0)

rest(f"/wp-json/wp/v2/posts/{PID}", "POST", {"status": "future", "date": TGL})
time.sleep(1)
d = rest(f"/wp-json/wp/v2/posts/{PID}?context=edit&_fields=status,slug,date,date_gmt,content,featured_media,link")
isi_sesudah = (d.get("content") or {}).get("raw", "")
print(f"sesudah: status={d.get('status')} | date={d.get('date')} | date_gmt={d.get('date_gmt')}")
print(f"   slug={d.get('slug')} | media={d.get('featured_media')} | isi={len(isi_sesudah)} char")
print("   isi utuh:", isi_sebelum == isi_sesudah)
print("   tombol WA:", "wa.me/6281935000624" in isi_sesudah, "| FAQPage:", "FAQPage" in isi_sesudah,
      "| h2:", len(re.findall(r"<h2>", isi_sesudah)))
print("   link:", d.get("link"))
