#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Kandidat artikel FRESH per domain utk Threads 5 Okt 2026.
Used set = SEMUA jadwal Repliz (semua akun/platform/status) + log_threads_harian.json.
Output: /tmp/kandidat_20261005.json
"""
import json, re
from urllib.parse import urlparse

def norm(u):
    u = u.strip().rstrip(").,;\"'")
    p = urlparse(u)
    return f"{p.netloc.lower().replace('www.','')}{p.path.rstrip('/').lower()}"

docs = json.load(open("/tmp/repliz_all_20261005.json"))
used = set()
per_acc = {}
from repliz_client import ACC  # noqa
import sys
sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
for d in docs:
    blob = json.dumps(d, ensure_ascii=False)
    for m in re.findall(r'https?://[\w\.\-/%\?#=&:]+', blob):
        used.add(norm(m))
    acc = d.get("accountId")
    per_acc.setdefault(acc, set())
    for m in re.findall(r'https?://[\w\.\-/%\?#=&:]+', blob):
        per_acc[acc].add(norm(m))
print(f"docs={len(docs)} used_global={len(used)} akun={len(per_acc)}")

log = json.load(open("/home/mantapman/.hermes/profiles/atun/workspace/log_threads_harian.json"))
for day, accs in (log.get("threads_used_links") or {}).items():
    for acc, items in accs.items():
        for it in items:
            used.add(norm(it["url"]))

DOM = {
    "studiosoft": "studiosoft.id", "uky": "studiosoft.id",
    "tslaw": "www.tslaw.id", "tsc": "www.theskincodeclinic.com", "das": "dasindonesia.com",
}
SOURCES = {
    "studiosoft.id": ["/tmp/live100_studiosoft.id.json", "/tmp/live_studiosoft.id.json", "/tmp/wp40_studiosoft.id.json"],
    "tslaw.id": ["/tmp/live100_www.tslaw.id.json", "/tmp/live_www.tslaw.id.json", "/tmp/wp40_www.tslaw.id.json"],
    "theskincodeclinic.com": ["/tmp/live100_www.theskincodeclinic.com.json", "/tmp/live_www.theskincodeclinic.com.json", "/tmp/wp40_www.theskincodeclinic.com.json"],
    "dasindonesia.com": ["/tmp/live100_dasindonesia.com.json", "/tmp/live_dasindonesia.com.json", "/tmp/wp40_dasindonesia.com.json"],
}

def title_of(p):
    t = p.get("title")
    return (t.get("rendered") if isinstance(t, dict) else t) or ""

pools = {}
for dom, files in SOURCES.items():
    seen = {}
    for fn in files:
        try:
            arr = json.load(open(fn))
        except Exception:
            continue
        for p in arr:
            n = norm(p["link"])
            if n not in seen:
                seen[n] = {"url": p["link"], "title": title_of(p), "date": (p.get("date") or "")[:10],
                           "status": p.get("status")}
    pools[dom] = seen

out = {}
for dom, seen in pools.items():
    fresh = [v for k, v in seen.items() if k not in used]
    fresh.sort(key=lambda x: x["date"], reverse=True)
    out[dom] = fresh
    print(f"\n## {dom}: {len(seen)} artikel total, {len(fresh)} FRESH (belum dipakai di jadwal manapun)")
    for a in fresh[:15]:
        print(f"   {a['date']} [{a.get('status')}] {a['title'][:70]}")
        print(f"        {a['url']}")

json.dump(out, open("/tmp/kandidat_20261005.json", "w"), indent=1, ensure_ascii=False)
print("\n-> /tmp/kandidat_20261005.json")
