#!/usr/bin/env python3
"""hc43: inventaris artikel studiosoft.id yang dipakai di LinkedIn SS & FB SS (dari dump 21 Sep)."""
import json, re, sys
sys.path.insert(0, '/home/mantapman/.hermes/profiles/atun/workspace')
from repliz_client import ACC

docs = json.load(open('/tmp/repliz_all_20260921.json'))
posts = json.load(open('/tmp/ss_posts_hc43.json'))
SLUGS = [p['slug'] for p in posts]

url_re = re.compile(r'studiosoft\.id/([a-z0-9\-/]+)')


def scan(aid, label):
    used = {}
    for d in docs:
        if d.get('accountId') != aid:
            continue
        t = " ".join([(d.get('description') or ''), (d.get('title') or '')])
        for m in url_re.findall(t):
            s = m.strip('/')
            used.setdefault(s, []).append(((d.get('scheduleAt') or '')[:16], d.get('status')))
    print("\n=== %s ===" % label)
    for s in SLUGS:
        rows = sorted(used.get(s, []))
        if not rows:
            print("  %-70s FREE" % s)
        else:
            succ = [r for r in rows if r[1] == 'success']
            pend = [r for r in rows if r[1] in ('pending', 'scheduled', 'draft')]
            err = [r for r in rows if r[1] not in ('success', 'pending', 'scheduled', 'draft')]
            print("  %-70s succ=%d pend=%s err=%d" % (s, len(succ), pend[:2], len(err)))


scan(ACC.STUDIOSOFT_LINKEDIN, "LINKEDIN SS (25 artikel terbaru WP)")
scan(ACC.STUDIOSOFT_FB, "FACEBOOK SS (25 artikel terbaru WP)")

print("\n=== jadwal LI/FB SS terdekat (pending) ===")
for aid, label in ((ACC.STUDIOSOFT_LINKEDIN, "LI"), (ACC.STUDIOSOFT_FB, "FB")):
    rows = []
    for d in docs:
        if d.get('accountId') != aid:
            continue
        if (d.get('status') or '') not in ('pending', 'scheduled', 'draft'):
            continue
        rows.append(((d.get('scheduleAt') or '')[:16], (d.get('title') or '')[:55]))
    rows.sort()
    print("--", label)
    for r in rows[:12]:
        print("   ", r)
