#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Audit penataan IG StudioSoft 3-14 Okt: ukuran media, pola warna dek, slot kosong."""
import sys, os, time, datetime, json
sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import get_client
from PIL import Image

IG_SS = "6a38ac33c5ff4ce3a3231232"
IMG = "/home/mantapman/.hermes/profiles/atun/workspace/openclaw-img"
WIB = datetime.timezone(datetime.timedelta(hours=7))

c = get_client()
docs, page = [], 1
while page <= 200:
    r = None
    for a in range(6):
        try:
            r = c.get_schedules(page=page, limit=100); break
        except Exception:
            time.sleep(3 + 2 * a)
    if r is None:
        print("fetch gagal hal", page); break
    data = r.get("data") or r.get("docs") or []
    docs.extend(data)
    if len(data) < 100:
        break
    page += 1

def dims(url):
    fn = url.split("/")[-1]
    p = os.path.join(IMG, fn)
    if not os.path.exists(p):
        return "FILE-HILANG"
    try:
        return Image.open(p).size
    except Exception as e:
        return f"ERR {e}"

rows = []
for d in docs:
    if (d.get("accountId") or (d.get("account") or {}).get("_id")) != IG_SS:
        continue
    at = d.get("scheduleAt") or ""
    try:
        dt = datetime.datetime.fromisoformat(at.replace("Z", "+00:00")).astimezone(WIB)
    except Exception:
        continue
    if datetime.date(2026, 10, 3) <= dt.date() <= datetime.date(2026, 10, 14):
        rows.append((dt, d))
rows.sort(key=lambda x: x[0])

hari = {}
for dt, d in rows:
    hari.setdefault(dt.date(), []).append((dt, d))

warna = None
try:
    from ss_carousel_lib import warna_ke  # type: ignore
    warna = warna_ke
except Exception as e:
    print("(warna_ke tidak tersedia:", e, ")")

for tgl in sorted(hari):
    print(f"\n### {tgl.strftime('%a %d %b')}  (paritas {'GENAP' if tgl.day % 2 == 0 else 'GANJIL'})")
    for dt, d in hari[tgl]:
        medias = d.get("medias") or []
        names = [m.get("url", "").split("/")[-1] for m in medias]
        info = []
        for n in names[:11]:
            info.append(f"{n}:{dims(os.path.join(IMG, n)) if os.path.splitext(n)[1].lower() not in ('.mp4',) else 'video'}")
        print(f"  {dt:%H:%M} {d.get('type'):6} {d.get('status'):8} n={len(medias)} " + " ".join(info)[:300])
