#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Perbaiki 1 jadwal bank DAS (14 Okt 19:00) yang memakai kata "Mitos".

Bos 6 Okt 2026: seri "Mitos atau Fakta" milik klien TS LAW — akun lain jangan memakai framing itu.
Caption bank diganti ke "Salah paham ...".

Pakai: python3 perbaiki_mitos_das_1410.py [--go]
"""
import sys, re, urllib.parse, urllib.request, json

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

DAS_THREADS = "6a39e461c5ff4ce3a3233e09"
go = "--go" in sys.argv

c = get_client()
docs, page = [], 1
while True:
    q = urllib.parse.urlencode({"page": page, "limit": 100})
    req = urllib.request.Request(f"{BASE}/public/schedule?{q}", headers=HEADERS)
    with urllib.request.urlopen(req, timeout=60) as r:
        d = json.loads(r.read().decode())
    b = d.get("docs") or []
    docs += b
    if not b or page * 100 >= (d.get("totalDocs") or 0):
        break
    page += 1

target = [x for x in docs if (x.get("account") or {}).get("_id") == DAS_THREADS
          and x.get("status") == "pending"
          and re.search(r"\bmitos\b", (x.get("description") or ""), re.I)]
print("jadwal DAS pending yang memuat 'mitos':", len(target))
for x in target:
    lama = x.get("description") or ""
    baru = re.sub(r"\bMitos\b", "Salah paham", lama, flags=re.I)
    print(f"  {x.get('scheduleAt')} | {x.get('title')} | {len(lama)} -> {len(baru)} char")
    print("   sebelum:", lama[:90].replace("\n", " "))
    print("   sesudah:", baru[:90].replace("\n", " "))
    if not go:
        continue
    c.update_schedule(x["_id"], {"description": baru})
    b2 = c.get_schedule(x["_id"])
    bb = b2.get("data", b2) if isinstance(b2, dict) else b2
    d2 = bb.get("description") or ""
    print(f"   -> {'✅ OK' if not re.search(r'mitos', d2, re.I) else '❌'} "
          f"status={bb.get('status')} len={len(d2)} masih_ada_mitos={bool(re.search(r'mitos', d2, re.I))}")
