#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Geser poster seri "Mitos atau Fakta: Penyitaan Barang" (tslaw_mitos_fakta_07)
dari 10 Okt -> 11 Okt 2026 (jam tetap: Threads 09:00, IG 11:00, FB 19:30 WIB).

Alasan: slot 09:00 Threads & 19:30 FB tanggal 10 Okt dipakai carousel 10-slide
"Mitos atau Fakta? 8 anggapan hukum" (perintah Bos: 2 hari setelah carousel PK tayang 8 Okt),
supaya tetap 1 post feed/hari/akun. Isi & media poster TIDAK diubah, cuma tanggalnya.

Repliz: PATCH selalu 404 -> pakai PUT dengan payload lengkap.
Dry run default; eksekusi: --go
"""
import sys, json, urllib.request

sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import HEADERS, BASE

TUJUAN = "2026-10-11"          # tanggal baru (WIB)
JAM = {"6a35f8b7c5ff4ce3a322b2ed": "02:00:00.000Z",   # Threads 09:00 WIB
       "6a76a54d62caae1e04f30968": "04:00:00.000Z",   # IG      11:00 WIB
       "6a3b41d1c5ff4ce3a32753e8": "12:30:00.000Z"}   # FB      19:30 WIB
IDS = ["6ab10f373032e3a79cf452d8", "6ab10b463032e3a79cf2e95e", "6ab10f3702078a414ace923a"]
go = "--go" in sys.argv


def get(id_):
    req = urllib.request.Request(f"{BASE}/public/schedule/{id_}", headers=HEADERS)
    with urllib.request.urlopen(req, timeout=60) as r:
        d = json.load(r)
    return d.get("doc", d)


def put(id_, payload):
    req = urllib.request.Request(f"{BASE}/public/schedule/{id_}", method="PUT",
                                 data=json.dumps(payload).encode(),
                                 headers={**HEADERS, "Content-Type": "application/json"})
    try:
        with urllib.request.urlopen(req, timeout=60) as r:
            return r.status, r.read()[:200]
    except Exception as e:
        return "ERR", f"{e} {getattr(e, 'read', lambda: b'')()[:200]}"


for id_ in IDS:
    doc = get(id_)
    acc = doc.get("accountId")
    baru = f"{TUJUAN}T{JAM[acc]}"
    print(f"{id_} | acc={acc} | {doc.get('scheduleAt')} -> {baru} | {doc.get('title')}")
    if not go:
        continue
    payload = {
        "accountId": acc,
        "title": doc.get("title"),
        "description": doc.get("description") or "",
        "topic": doc.get("topic") or "Edukasi Hukum",
        "type": doc.get("type") or "image",
        "medias": doc.get("medias") or [],
        "scheduleAt": baru,
        "replies": doc.get("replies") or [],
    }
    st, body = put(id_, payload)
    cek = get(id_)
    print(f"   PUT={st} -> sekarang {cek.get('scheduleAt')} | status={cek.get('status')} | "
          f"media={len(cek.get('medias') or [])}")

if not go:
    print("\n(dry run — tambahkan --go)")
