#!/usr/bin/env python3
"""Cek kandidat LinkedIn (0 hit di semua jadwal LI) + ketersediaan slot Threads/FB/LI."""
import json, re, sys
from datetime import datetime, timezone, timedelta
sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import ACC

docs = json.load(open("/tmp/hc65_sched.json"))
now = datetime.now(timezone.utc)
NOW = now.strftime("%Y-%m-%dT%H:%M")

LI = ACC.STUDIOSOFT_LINKEDIN
FB = ACC.STUDIOSOFT_FB
THR = ACC.STUDIOSOFT_THREADS

# kandidat LI: artikel terbaru (dari posts), non-HC, cek hit di SEMUA jadwal LI
posts = json.load(open("/tmp/hc65_posts.json"))
li_blob = json.dumps([d for d in docs if d.get("accountId") == LI], ensure_ascii=False)
print("=== kandidat LinkedIn (baru -> lama, 0 hit di LI) ===")
for p in posts[:16]:
    slug = p["slug"]
    n = len(re.findall(re.escape(slug), li_blob))
    print(f"  {p['date'][:10]} hits_LI={n:>2}  {slug}")

print("\n=== slot Threads 02:00Z (09:00 WIB) sibuk? ===")
busy_thr = {str(d.get("scheduleAt"))[:19] for d in docs
            if d.get("accountId") == THR and (d.get("status") or "").lower() in ("pending", "scheduled", "draft")}
for day in range(0, 5):
    d0 = (now + timedelta(days=day)).strftime("%Y-%m-%d")
    c = d0 + "T02:00:00"
    print(f"  {c} -> {'BUSY' if c in busy_thr else 'FREE'}")

print("\n=== slot FB 12:30Z (19:30 WIB) ===")
busy_fb = {str(d.get("scheduleAt"))[:19] for d in docs
           if d.get("accountId") == FB and (d.get("status") or "").lower() in ("pending", "scheduled", "draft")}
for day in range(0, 9):
    d0 = (now + timedelta(days=day)).strftime("%Y-%m-%d")
    c = d0 + "T12:30:00"
    print(f"  {c} -> {'BUSY' if c in busy_fb else 'FREE'}")

print("\n=== slot LI (grid 01/04/07/10/13/16Z) 6 slot terdekat bebas ===")
busy_li = {str(d.get("scheduleAt"))[:19] for d in docs
           if d.get("accountId") == LI and (d.get("status") or "").lower() in ("pending", "scheduled", "draft")}
cands = []
for day in range(0, 12):
    d0 = datetime(now.year, now.month, now.day, tzinfo=timezone.utc) + timedelta(days=day)
    for h in (1, 4, 7, 10, 13, 16):
        dt = d0 + timedelta(hours=h)
        if dt <= now + timedelta(minutes=30):
            continue
        cs = dt.strftime("%Y-%m-%dT%H:%M:%S")
        if cs not in busy_li:
            cands.append(cs)
for c in cands[:10]:
    wib = (datetime.fromisoformat(c) + timedelta(hours=7)).strftime("%d %b %Y %H:%M WIB")
    print(f"  {c}Z  ({wib})")

print("\n=== LI sibuk terdekat (5) ===")
for c in sorted(busy_li)[:5]:
    print("  ", c)
