#!/usr/bin/env python3
"""hc67: cek jejak slug kandidat (baru + resurrected) di dump jadwal StudioSoft."""
import json, re, sys
from datetime import datetime, timezone
sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import ACC

docs = json.load(open('/tmp/hc67_sched.json'))
ACC_MAP = {ACC.STUDIOSOFT_THREADS: "THR", ACC.STUDIOSOFT_LINKEDIN: "LI",
           ACC.STUDIOSOFT_FB: "FB", ACC.STUDIOSOFT_IG: "IG"}
slugs = [
    "cara-memilih-digital-agency-surabaya",
    "website-rumah-sakit-gagal-pasien",
    "konten-edukasi-kesehatan-kepercayaan-pasien",
]
now = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M")
for s in slugs:
    pat = re.compile(re.escape(s))
    hits = []
    for d in docs:
        plat = ACC_MAP.get(d.get('accountId'))
        if not plat:
            continue
        blob = json.dumps({k: d.get(k) for k in ('title', 'description', 'topic')}, ensure_ascii=False)
        for m in (d.get('medias') or []):
            blob += " " + (m.get('url') or '')
        if pat.search(blob):
            hits.append((plat, str(d.get('scheduleAt'))[:16], d.get('status'), d.get('type'),
                         (d.get('_id') or d.get('id') or '')[:12]))
    hits.sort()
    fut = [h for h in hits if h[1] >= now and h[2] in ('pending', 'scheduled', 'draft')]
    print("\n%s: total=%d future=%d" % (s, len(hits), len(fut)))
    for h in hits:
        print("   ", h)
print("\nnow:", now)

# LI antrean: slot terisi
import collections
li = [d for d in docs if d.get('accountId') == ACC.STUDIOSOFT_LINKEDIN
      and (d.get('status') or '').lower() in ('pending', 'scheduled', 'draft')]
li.sort(key=lambda d: str(d.get('scheduleAt')))
print("\nLI pending/scheduled total:", len(li))
for d in li[:12]:
    print("   ", str(d.get('scheduleAt'))[:16], (d.get('title') or '')[:60])
print("   ... terakhir:", str(li[-1].get('scheduleAt'))[:16] if li else None)

fb = [d for d in docs if d.get('accountId') == ACC.STUDIOSOFT_FB
      and (d.get('status') or '').lower() in ('pending', 'scheduled', 'draft')]
fb.sort(key=lambda d: str(d.get('scheduleAt')))
print("\nFB pending/scheduled total:", len(fb))
for d in fb:
    print("   ", str(d.get('scheduleAt'))[:16], (d.get('title') or '')[:60])

thr = [d for d in docs if d.get('accountId') == ACC.STUDIOSOFT_THREADS
       and (d.get('status') or '').lower() in ('pending', 'scheduled', 'draft')]
thr.sort(key=lambda d: str(d.get('scheduleAt')))
print("\nTHREADS pending/scheduled total:", len(thr))
for d in thr[:15]:
    print("   ", str(d.get('scheduleAt'))[:16], (d.get('title') or '')[:60])
