#!/usr/bin/env python3
"""Audit duplikat seri uky5: bandingkan judul+body tiap kartu baru dengan SELURUH korpus jadwal
Repliz (description + topic) + caption di image_posting_log.json. READ-ONLY.
Skor = Jaccard kata isi (>=4 huruf) vs deskripsi/judul korpus.
"""
import json, os, re, importlib.util

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
spec = importlib.util.spec_from_file_location("g5", os.path.join(WS, "gen_uky5_quotes.py"))
g5 = importlib.util.module_from_spec(spec)
spec.loader.exec_module(g5)

corpus = json.load(open(os.path.join(WS, "_recon_dump_20261017.json")))
log = json.load(open(os.path.join(WS, "image_posting_log.json")))

STOP = set("""yang dan di ke dari untuk pada dengan itu ini tidak bukan saya aku kamu kita kami
akan sudah ada adalah atau juga saja cuma tapi tapi kalau saat setelah sebelum lebih bukan
orang kerja kerjaan cara bikin buat kalau mau harus bisa tidak nya lah pun se ya nya""".split())

def words(s):
    ws = re.findall(r"[a-z0-9]{4,}", (s or "").lower())
    return {w for w in ws if w not in STOP}

docs = []
for d in corpus:
    docs.append((d.get("id"), d.get("scheduleAt"), d.get("accountId"), (d.get("description") or ""),
                 d.get("topic") or ""))
for e in log:
    docs.append(("LOG:" + str(e.get("item_id")), e.get("timestamp"), "", e.get("caption_final") or "", ""))

for k in g5.LIST_POSTINGAN:
    src = words(k["judul"] + " " + k["body"])
    scored = []
    for did, at, aid, desc, topic in docs:
        tgt = words(desc + " " + topic)
        if not tgt:
            continue
        inter = src & tgt
        j = len(inter) / max(1, len(src | tgt))
        if j > 0.02:
            scored.append((j, did, at, inter, (desc or topic)[:90].replace("\n", " ")))
    scored.sort(reverse=True)
    top = scored[:3]
    flag = "!!" if top and top[0][0] > 0.14 else "ok"
    print(f"{flag} {k['nama']:<28} kemiripan tertinggi:", f"{top[0][0]:.3f}" if top else "-")
    for j, did, at, inter, snip in top:
        print(f"      {j:.3f} {did} {at} | {sorted(inter)} | {snip}")
