#!/usr/bin/env python3
"""DRY-RUN jadwal full Agustus — verifikasi tanpa POST ke Repliz.
Cek: (1) artikel unik, (2) caption Threads ≤500, (3) jadwal per akun masuk akal,
(4) sample caption tiap platform, (5) stok cukup.
"""
import sys, json, re
from datetime import datetime, timedelta
sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import HEADERS, BASE
import importlib.util

# load module tanpa jalanin main
spec = importlib.util.spec_from_file_location("jfa", "jadwal_full_agustus.py")
jfa = importlib.util.module_from_spec(spec)
# patch main biar gak jalan
import types
jfa.main = lambda: None
spec.loader.exec_module(jfa)

print("=" * 70)
print("🧪 DRY-RUN — Verifikasi Jadwal Full Agustus (6-31 Agustus)")
print("=" * 70)
print(f"Stok artikel: { {k: len(v) for k, v in jfa.STOK.items()} }")
print(f"Used links dari Repliz: {len(jfa.used_links)} (sudah di-exclude)")
print()

# simulasi penuh
start = datetime(2026, 8, 6)
end = datetime(2026, 8, 31)
used_in_sim = set()
problems = []
stats = {}
samples = {}
d = start
total_posts = 0
while d <= end:
    for acc_key, wib in jfa.day_schedule(d):
        brand = jfa.BRAND_OF[acc_key]
        art = jfa.take_article(brand)
        total_posts += 1
        if not art:
            problems.append(f"{d.date()} {acc_key}: stok {brand} HABIS")
            continue
        key = art["link"].replace("www.", "").rstrip("/")
        if key in used_in_sim:
            problems.append(f"DUPLIKAT! {acc_key} {d.date()}: {art['link']}")
        used_in_sim.add(key)
        variant = stats.get(acc_key, 0)
        stats[acc_key] = variant + 1
        if acc_key.endswith("_th"):
            text = jfa.cap_threads(brand, art, variant)
            if len(text) > 500:
                problems.append(f"Threads >500 ({len(text)}): {acc_key} {d.date()}")
        elif acc_key.endswith("_fb"):
            text = jfa.cap_fb(brand, art, variant)
        else:
            text = jfa.cap_li(brand, art, variant)
        if acc_key not in samples:
            ttl = art["title"]
            if isinstance(ttl, dict):
                ttl = ttl.get("rendered") or ""
            samples[acc_key] = (d.date().isoformat(), wib, ttl, text[:120].replace("\n", " "))
    d += timedelta(days=1)

print(f"📊 Total post terjadwal: {total_posts}")
print(f"📊 Unik artikel dipakai: {len(used_in_sim)}")
print()
print("📋 Rincian per akun:")
for k, v in sorted(stats.items()):
    print(f"   {k}: {v} post")
print()
if problems:
    print(f"❌ {len(problems)} MASALAH DITEMUKAN:")
    for p in problems[:30]:
        print(f"   - {p}")
else:
    print("✅ TIDAK ADA MASALAH: semua artikel unik, semua caption ≤500 (Threads)")
print()
print("👀 SAMPLE CAPTION:")
for k, (dt, wib, title, txt) in list(samples.items())[:13]:
    print(f"\n--- {k} @ {dt} {wib} | {title[:60]} ---")
    print(f"   {txt}...")
