#!/usr/bin/env python3
"""Hitung artikel BELUM PERNAH dipakai di Threads (per akun), dari cache WP + log + cache jadwal."""
import json, re, sys
sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
ACCOUNTS = {"studiosoft": "6a335b9ec5ff4ce3a3225c2a", "uky": "6a334e89c5ff4ce3a3225a48",
            "tslaw": "6a35f8b7c5ff4ce3a322b2ed", "tsc": "6a4bbc5920e4f4f195e52960",
            "das": "6a39e461c5ff4ce3a3233e09"}

artikel = json.load(open(f"{WS}/_cache_artikel_wp_17okt.json"))
sched = json.load(open(f"{WS}/_cache_sched_threads_17okt.json"))
log = json.load(open(f"{WS}/log_threads_harian.json"))

used = set()
# 1) log links
for day, per_acc in (log.get("threads_used_links") or {}).items():
    for acc, items in per_acc.items():
        for it in items:
            u = it.get("url") if isinstance(it, dict) else it
            if u:
                used.add(u.rstrip("/").lower())
# 2) semua URL di caption jadwal Threads (semua status, semua tanggal)
tids = set(ACCOUNTS.values())
for d in sched:
    if d.get("accountId") in tids:
        for u in re.findall(r"https?://\S+", d.get("description") or ""):
            used.add(u.rstrip(".,) ").rstrip("/").lower())

print(f"total URL pernah dipakai di Threads: {len(used)}")
out = {}
for key, items in artikel.items():
    fresh = []
    for it in items:
        u = (it.get("link") or "").rstrip("/").lower()
        if u and u not in used:
            fresh.append(it)
    fresh.sort(key=lambda x: x.get("date") or "", reverse=True)
    out[key] = fresh
    print(f"\n=== {key}: {len(fresh)} FRESH / {len(items)} total")
    for it in fresh[:12]:
        print(f"   {(it.get('date') or '')[:10]}  {it.get('link')}")
        print(f"        {re.sub('<[^>]+>', '', it.get('title') or '')[:110]}")

json.dump(out, open(f"{WS}/_cache_artikel_fresh_17okt.json", "w"), ensure_ascii=False, indent=1)
print("\nsaved -> _cache_artikel_fresh_17okt.json")
