#!/usr/bin/env python3
"""hc57: cek artikel terbaru vs antrean LI + slot bebas LI 7-grid."""
import json, re, sys
from datetime import datetime, timedelta, timezone
WS = "/home/mantapman/.hermes/profiles/atun/workspace"
sys.path.insert(0, WS)
from repliz_client import ReplizClient, ACC

docs = json.load(open("/tmp/repliz_all_20261006.json"))
url_re = re.compile(r"studiosoft\.id/([a-z0-9\-]+)")


def slugs_of(d):
    return url_re.findall((d.get("description") or "") + " " + (d.get("title") or ""))


def st(d):
    return (d.get("status") or "").lower()


li_pend = set()
li_succ = set()
for d in docs:
    if d.get("accountId") != ACC.STUDIOSOFT_LINKEDIN:
        continue
    if st(d) in ("pending", "scheduled", "draft"):
        li_pend |= set(slugs_of(d))
    if st(d) == "success":
        li_succ |= set(slugs_of(d))

new = ["branding-agency-surabaya-proses-hasil-kerja", "marketing-industri",
       "tools-analisis-kompetitor-media-sosial", "jasa-seo-surabaya-paket-harga",
       "konsultan-pemasaran-digital", "digital-marketing-rumah-sakit-indikator",
       "seo-rumah-sakit", "jasa-digital-marketing-rs", "pembuatan-website-rumah-sakit",
       "tips-seo-digital-marketing-surabaya-optimasi-local-untuk-klinik-dan-bengkel-lokal"]
print("== artikel vs antrean LI ==")
for s in new:
    print("  %-70s pend=%-5s succ=%s" % (s[:70], s in li_pend, s in li_succ))

# slot bebas LI (7 grid) setelah sekarang
busy = set()
for d in docs:
    if d.get("accountId") == ACC.STUDIOSOFT_LINKEDIN and st(d) in ("pending", "scheduled", "draft"):
        busy.add((d.get("scheduleAt") or "")[:19])
now = datetime.now(timezone.utc)
print("\n== slot LI bebas (7 grid: 22,01,04,07,10,13,16 UTC) ==")
found = 0
for day in range(0, 21):
    d0 = datetime(now.year, now.month, now.day, tzinfo=timezone.utc) + timedelta(days=day)
    for h in (22, 1, 4, 7, 10, 13, 16):
        dt = d0 + timedelta(hours=h)
        if dt <= now + timedelta(minutes=20):
            continue
        cand = dt.strftime("%Y-%m-%dT%H:%M:%S")
        if cand not in busy:
            wib = (dt + timedelta(hours=7)).strftime("%d %b %Y %H:%M WIB")
            print("  FREE %s (UTC %s)" % (wib, cand))
            found += 1
            if found >= 8:
                break
    if found >= 8:
        break
