#!/usr/bin/env python3
"""Cek media 2 ingress + peta jam jadwal pending IG SS (read-only)."""
import subprocess, sys, collections, datetime
sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import ReplizClient, ACC

IPS = ["103.84.155.153", "103.84.155.217"]
HOST = "mantapserver.tail3947bb.ts.net"

def cek(url, ip):
    r = subprocess.run([
        "curl", "-s", "-o", "/dev/null", "-w", "%{http_code}", "--max-time", "25",
        "--resolve", f"{HOST}:443:{ip}", url], capture_output=True, text=True)
    return r.stdout.strip()

print("=== media carousel 14 Okt ===")
bad = []
for i in range(1, 11):
    url = f"https://{HOST}/openclaw-img/ss_car10_promptai_{i:02d}.jpg"
    codes = [cek(url, ip) for ip in IPS]
    print(f"{i:02d}: {codes}")
    if any(c != "200" for c in codes):
        bad.append((i, codes))
print("BUKAN 200:", bad if bad else "tidak ada")

print()
print("=== story mirror 08:40 ===")
url = f"https://{HOST}/openclaw-img/ss_story_canvas_ss_car10_promptai_01.jpg"
print([cek(url, ip) for ip in IPS])

print()
print("=== peta jam jadwal IG SS (pending ke depan) ===")
c = ReplizClient()
acc = ACC.STUDIOSOFT_IG
rows = []
page = 1
while True:
    r = c.get_schedules(page=page, limit=100)
    docs = r.get("docs") or r.get("data") or []
    if not docs:
        break
    for d in docs:
        a = d.get("account") or {}
        if (a.get("_id") or a.get("id")) == acc:
            rows.append(d)
    if not r.get("hasNextPage"):
        break
    page += 1
    if page > 40:
        break

now = datetime.datetime(2026, 10, 14, 2, 0, tzinfo=datetime.timezone.utc)
peta = collections.Counter()
jam_terakhir = {}
for d in rows:
    if d.get("status") != "pending":
        continue
    sa = d.get("scheduleAt") or ""
    try:
        dt = datetime.datetime.fromisoformat(sa.replace("Z", "+00:00"))
    except Exception:
        continue
    if dt < now:
        continue
    wib = dt + datetime.timedelta(hours=7)
    tipe = d.get("type")
    peta[(tipe, f"{wib.hour:02d}:{wib.minute:02d}")] += 1
    key = (tipe, f"{wib.hour:02d}:{wib.minute:02d}")
    if key not in jam_terakhir or wib > jam_terakhir[key]:
        jam_terakhir[key] = wib

for k in sorted(peta, key=lambda x: (x[1], x[0])):
    print(f"  {k[1]} WIB | {k[0]:6} | {peta[k]:3}x | terakhir {jam_terakhir[k].date()}")
