#!/usr/bin/env python3
"""Tambahkan aturan diskusi ke prompt cron harian (FIXED 7 Agustus 2026)."""
import json, subprocess, sys

RULES = """

## 🗣️ ATURAN DISKUSI (FIXED 7 Agustus 2026)
Akhiri caption dengan PANCINGAN DISKUSI supaya interaksi ada (bukan konten searah):
- 60% netral: pertanyaan terbuka / ajak share pengalaman ("Menurut Anda bagaimana? Yuk diskusi di komentar.")
- 25% opini: pernyataan berani / pro-kontra ringan yang SOPAN ("Bold statement: banyak orang menyepelekan ini. Setuju atau tidak?")
- 15% sanjungan: apresiasi ke audiens ("Buat yang sudah konsisten menerapkan ini, Anda selangkah lebih maju! 👏")
Tools: python3 diskusi_engine.py <industri> <tone> — industri: seo, digital_marketing, klinik, rumah_sakit, apotek, hukum, pendidikan, otomotif, kreatif, personal_branding, kesehatan, kecantikan, teknologi, umum. Tone: netral/opini/sanjungan.
Aturan lain tetap berlaku: Threads ≤500 char, link wajib valid (200), konten unik, bahasa Indonesia, sopan, JANGAN serang kompetitor/SARA/politik."""

# (id, nama)
CRONS = [
    ("45be2e3c-463f-4894-8b09-6bce4442b4b0", "dmark-fb-harian"),
    ("ec4b7c5e-f9e0-447d-83fe-19c13fe18ed6", "dmark-linkedin-harian"),
    ("93125749-1b50-4d64-a225-618d8fe8d494", "fb-das-harian"),
    ("55451782-6c27-46d8-aaef-3a1e6cee7fa2", "leit-fb-linkedin-harian"),
    ("47423215-f24e-480f-92aa-b4db4772d19e", "tslaw-linkedin-harian"),
    ("847df9e7-3e81-4b09-bf92-9446efaf8838", "threads-all-harian"),
    ("d8bba96e-f61e-4a85-823a-16292227a3e3", "studiosoft-healthcare-carousel-harian"),
    ("b4ac42cd-b43e-438c-857f-1cffe3fe442f", "daily-image-posting-all"),
]

for cid, name in CRONS:
    # ambil message lama
    out = subprocess.run(["openclaw", "cron", "get", cid], capture_output=True, text=True)
    try:
        d = json.loads(out.stdout)
        old = d.get("payload", {}).get("message", "")
    except Exception as e:
        print(f"❌ {name}: gagal baca cron ({e})")
        continue

    if "ATURAN DISKUSI" in old:
        print(f"⏭️  {name}: sudah ada aturan diskusi, skip")
        continue

    new_msg = old.rstrip() + RULES
    # tulis ke file temp biar aman dari masalah quoting
    tmp = f"/tmp/cron_msg_{cid}.txt"
    with open(tmp, "w") as f:
        f.write(new_msg)

    r = subprocess.run(
        ["openclaw", "cron", "edit", cid, "--message", f"$(cat {tmp})"],
        capture_output=True, text=True, shell=True
    )
    # cek hasil
    check = subprocess.run(["openclaw", "cron", "get", cid], capture_output=True, text=True)
    try:
        d2 = json.loads(check.stdout)
        has = "ATURAN DISKUSI" in d2.get("payload", {}).get("message", "")
        print(f"{'✅' if has else '❌'} {name}: aturan diskusi {'terpasang' if has else 'GAGAL'}")
    except Exception as e:
        print(f"⚠️ {name}: perlu verifikasi manual ({e})")
