#!/usr/bin/env python3
"""Verifikasi independen 4 jadwal 15 Okt 2026 + tulis note ke log."""
import json, re, sys
sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import get_client

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
IDS = {
 "uky": "6acfede5aaeff6487c189973",
 "tslaw": "6acfede5af766377d4cd55ff",
 "tsc": "6acfede6af766377d4cd5607",
 "das": "6acfede6af766377d4cd560f",
}
c = get_client()
report = []
for k, sid in IDS.items():
    d = c.get_schedule(sid)
    doc = d.get("doc") or d if isinstance(d, dict) else d
    if isinstance(doc, dict) and "doc" in doc:
        doc = doc["doc"]
    desc = doc.get("description") or ""
    links = re.findall(r"https?://\S+", desc)
    cjk = [ch for ch in desc if "\u4e00" <= ch <= "\u9fff" or "\u3040" <= ch <= "\u30ff" or "\uac00" <= ch <= "\ud7af"]
    ment = re.findall(r"(?<!\S)@\w+", desc)
    print(f"{k:6s} id={doc.get('_id')} status={doc.get('status')} type={doc.get('type')} at={doc.get('scheduleAt')}")
    print(f"       topic={doc.get('topic')!r} title={doc.get('title')!r}")
    print(f"       len={len(desc)} links={links} replies={len(doc.get('replies') or [])} mention={ment} cjk={cjk} medias={len(doc.get('medias') or [])}")
    print(f"       tail={desc[-90:]!r}")
    report.append({"account": k, "id": doc.get("_id"), "status": doc.get("status"),
                   "scheduleAt": doc.get("scheduleAt"), "topic": doc.get("topic"),
                   "title": doc.get("title"), "len": len(desc),
                   "link": links[0] if links else None,
                   "mentions": ment, "cjk": cjk, "replies": len(doc.get("replies") or [])})
json.dump(report, open(f"{WS}/_verif_final_15okt.json", "w"), indent=1, ensure_ascii=False)

note = ("15 Okt 2026 (run 04:00 WIB): StudioSoft SKIP — 5 jadwal same-day (05:00 bank percakapan tanpa link, "
        "08:00 text ber-link /digital-marketing-retail/, album 09:00 + anak 09:01/09:02 ber-link) → guard maks 1 konten/hari/akun, "
        "dan SS sudah punya post ber-link sendiri hari ini. Dibuat 4 post artikel ber-link: "
        "uky 14:00 id 6acfede5aaeff6487c189973 (471 char, topic 'Belajar & Skill', opt-in allow_if_no_ownlink: 4 jadwal same-day "
        "09:00 album / 09:35 / 12:05 / 20:00 semua TANPA link studiosoft.id) · "
        "tslaw 17:00 id 6acfede5af766377d4cd55ff (486 char, topic 'Hak Cipta', TANPA jadwal same-day sebelumnya) · "
        "tsc 20:00 id 6acfede6af766377d4cd5607 (499 char, topic 'Keluhan Kulit & Kelamin', opt-in: 17:00 album + 19:00 tanpa link) · "
        "das 20:00 id 6acfede6af766377d4cd560f (471 char, topic 'Perawatan Mobil', opt-in: bank percakapan 07:00 & 19:00 tanpa link — "
        "dua slot itu TIDAK disentuh). Semua terverifikasi GET /public/schedule/<id>: status pending, type=text, replies=0, medias=0, "
        "0 mention, link ada di ekor, CTA komentar ada, 0 CJK, topic <=50 char. Link FRESH (0 kemunculan di 1.922 jadwal Repliz + log): "
        "uky=studiosoft.id/6-skill-wajib-untuk-belajar-digital-marketing-bagi-pemula-di-tahun-2026 (stok SS: 50 artikel blm terpakai dari 328), "
        "tslaw=tslaw.id/cara-daftar-hak-cipta, tsc=theskincodeclinic.com/keluhan-kulit-kelamin-yang-ditangani-dokter-spesialis (publish 14 Okt), "
        "das=dasindonesia.com/6-tips-cara-mudah-merawat-mobil-kesayangan-anda (publish 14 Okt). Keempat link HTTP 200 dari host ini (openssl s_client). "
        "Script: _recon_15okt.py, _kandidat_15okt.py, _fresh_ss_15okt.py, _verif_isi_15okt.py, _build_15okt.py.")

lg = json.load(open(f"{WS}/log_threads_harian.json"))
if not any(p.get("note", "").startswith("15 Okt 2026 (run 04:00 WIB)") for p in lg["posts"]):
    lg["posts"].append({"date": "2026-10-15", "note": note})
    json.dump(lg, open(f"{WS}/log_threads_harian.json", "w"), indent=2, ensure_ascii=False)
    print("\n✅ note 15 Okt ditulis ke log")
else:
    print("\nℹ️ note sudah ada")
