#!/usr/bin/env python3
"""Lanjutan: buat ulang 5 jadwal yang CREATE-nya gagal HTTP 500 (21 Sep 2026).

Jadwal lama SUDAH terhapus (DELETE sukses, CREATE gagal) → payload diambil dari
`repliz_all_dump.json` (snapshot 10:29 UTC, sebelum penghapusan). Retry 4x + jeda.
"""
import json
import sys
import time
from datetime import datetime, timedelta, timezone

sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import get_client  # noqa: E402

IDS = [
    "6a9c97b586a732623afe4510",  # studiosoft_ig  album
    "6a94e30ccd18a9f6abc2f7e9",  # dojo_ksatria_ig video
    "6aace2eafc107e4fe5f0b2df",  # tsc_threads   image
    "6a81e1e216c9335a365e4131",  # leit_ig       image
    "6a82c84516c9335a3681e570",  # leit_tiktok   image
]
WIB = timezone(timedelta(hours=7))
c = get_client()
docs = json.load(open("/home/mantapman/.hermes/profiles/atun/workspace/repliz_all_dump.json"))
base = datetime.now(timezone.utc).replace(second=0, microsecond=0) + timedelta(minutes=8)

for i, sid in enumerate(IDS):
    d = next((x for x in docs if x.get("id") == sid), None)
    if not d:
        print(f"❌ payload {sid} tidak ada di dump — dilewati")
        continue
    at = base + timedelta(minutes=3 * i)
    name = ((d.get("account") or {}).get("name") or "?")[:34]
    for attempt in range(1, 5):
        try:
            res = c.create_schedule(
                d["accountId"], d.get("description") or "",
                at.strftime("%Y-%m-%dT%H:%M:%S.000Z"),
                title=d.get("title") or "", topic=d.get("topic") or "",
                type_=d.get("type") or "text", medias=d.get("medias") or [],
                replies=d.get("replies") or [], meta=d.get("meta"),
                additional_info=d.get("additionalInfo"))
            nid = res.get("scheduleId") or res.get("_id") or res.get("id")
            chk = c.get_schedule(nid)
            chk = chk.get("doc") or chk
            print(f"✅ {name:34} | lama {sid} -> {nid} | {at.astimezone(WIB):%H:%M} WIB | "
                  f"status={chk.get('status')} | type={chk.get('type')} | medias={len(chk.get('medias') or [])}")
            break
        except Exception as e:
            print(f"  ⚠️ {name} attempt {attempt}: {e}")
            time.sleep(15)
    else:
        print(f"❌ {name} gagal 4x ({sid}) — payload masih tersimpan di repliz_all_dump.json")
