#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Cek horizon carousel 08:30 IG SS (s/d 31 Okt) + health media 2 ingress."""
import sys, time, datetime, subprocess
WS = "/home/mantapman/.hermes/profiles/atun/workspace"
sys.path.insert(0, WS)
from repliz_client import get_client

IG_SS = "6a38ac33c5ff4ce3a3231232"
WIB = datetime.timezone(datetime.timedelta(hours=7))
c = get_client()

docs, page = [], 1
while page <= 80:
    r = None
    for a in range(5):
        try:
            r = c.get_schedules(page=page, limit=100); break
        except Exception:
            time.sleep(3 + 2 * a)
    if r is None:
        raise SystemExit("gagal baca")
    b = r.get("docs") or []
    docs += b
    if len(b) < 100:
        break
    page += 1

mine = [d for d in docs if (d.get("account") or {}).get("id") == IG_SS or d.get("accountId") == IG_SS]

def wib(s):
    try:
        return datetime.datetime.fromisoformat(s.replace("Z", "+00:00")).astimezone(WIB)
    except Exception:
        return None

alb = {}
for d in mine:
    w = wib(d.get("scheduleAt", ""))
    if w and w.hour == 8 and w.minute == 30 and d.get("type") == "album":
        alb.setdefault(w.date(), []).append(d)

print("hari dengan carousel 08:30 (Okt):")
d0 = datetime.date(2026, 10, 12)
d1 = datetime.date(2026, 10, 31)
x = d0
hilang = []
while x <= d1:
    n = len(alb.get(x, []))
    if n == 0:
        hilang.append(x)
    x += datetime.timedelta(days=1)
print("  jumlah hari:", (d1 - d0).days + 1)
print("  hari TANPA carousel 08:30:", [str(h) for h in hilang] or "tidak ada")

# salud media 12 Okt
d = alb[datetime.date(2026, 10, 12)][0]
urls = [m.get("url") for m in (d.get("medias") or [])]
print("\nhealth media 12 Okt (2 ingress):")
bad = 0
for u in urls:
    codes = []
    for ip in ("103.84.155.153", "103.84.155.217"):
        p = subprocess.run(["curl", "-sk", "-o", "/dev/null", "-w", "%{http_code}",
                            "--max-time", "25", "--resolve",
                            f"mantapserver.tail3947bb.ts.net:443:{ip}", u],
                           capture_output=True, text=True)
        codes.append(p.stdout.strip())
    ok = all(x == "200" for x in codes)
    if not ok:
        bad += 1
    print(f"  {'OK ' if ok else 'BAD'} {codes}  {u.split('/')[-1]}")
print("media bermasalah:", bad)
