#!/usr/bin/env python3
"""Lihat ritme terbit artikel StudioSoft: jadwal yang masih menunggu (future), draf, dan cadence harian."""
import base64, json, urllib.request
from datetime import datetime, timezone, timedelta

S = "https://studiosoft.id"
AUTH = base64.b64encode(b"bambangbot:q26r RePo BVy6 pwU2 F3z7 ouyl").decode()
UA = {"User-Agent": "Mozilla/5.0"}
WIB = timezone(timedelta(hours=7))


def rest(path):
    h = {"Authorization": f"Basic {AUTH}", **UA}
    r = urllib.request.Request(S + path, headers=h)
    with urllib.request.urlopen(r, timeout=120) as x:
        return json.loads(x.read()), dict(x.headers)


def daftar(status):
    keluar, page = [], 1
    while True:
        d, hdr = rest(f"/wp-json/wp/v2/posts?status={status}&per_page=100&page={page}"
                      f"&orderby=date&order=desc&_fields=id,slug,link,date,status,title")
        if not isinstance(d, list) or not d:
            break
        keluar += d
        if len(d) < 100:
            break
        page += 1
    return keluar


future = daftar("future")
draf = daftar("draft")
terbit = daftar("publish")[:25]

print(f"=== menunggu tayang (future): {len(future)} ===")
for p in sorted(future, key=lambda x: x["date"]):
    tgl = datetime.fromisoformat(p["date"]).astimezone(WIB)
    print(f"  #{p['id']:>5} {tgl.strftime('%a %d %b %H:%M')} WIB | {p['slug'][:60]}")

print(f"\n=== draf: {len(draf)} ===")
for p in draf[:20]:
    print(f"  #{p['id']:>5} {p['date'][:10]} | {p['slug'][:60]}")

print("\n=== 25 artikel terakhir terbit ===")
hari = {}
for p in terbit:
    tgl = datetime.fromisoformat(p["date"]).astimezone(WIB)
    hari.setdefault(tgl.date().isoformat(), []).append((tgl.strftime("%H:%M"), p["id"], p["slug"][:52]))
for t in sorted(hari, reverse=True):
    for jam, pid, slug in sorted(hari[t]):
        print(f"  {t} {jam} | #{pid:>5} | {slug}")
