#!/usr/bin/env python3
"""Verifikasi jadwal DAS FB 12 Sep 2026 dari API + history."""
import json, re, sys, urllib.request
from repliz_client import HEADERS, BASE, ACC

SID = "6aa523d9703d29cb5858fc0c"
URL = "https://dasindonesia.com/bengkel-buka-hari-minggu-surabaya"
IMG = "https://mantapserver.tail3947bb.ts.net/openclaw-img/das_fb_20260912.jpg"

found = None
page = 1
pend_today = []
while True:
    req = urllib.request.Request(f'{BASE}/public/schedule?page={page}&limit=100', headers=HEADERS, method='GET')
    d = json.loads(urllib.request.urlopen(req, timeout=60).read())
    for x in d.get('docs', []):
        if x.get('accountId') != ACC.DAS_FB:
            continue
        if (x.get('scheduleAt') or '').startswith('2026-09-12') and x.get('status') in ('pending', 'draft'):
            pend_today.append((x.get('scheduleAt'), x.get('_id'), x.get('type')))
        if x.get('_id') == SID:
            found = x
    if page >= d.get('totalPages', 1):
        break
    page += 1

if not found:
    print("❌ jadwal tidak ketemu di API")
    sys.exit(1)

desc = found.get('description') or ''
print("id          :", found.get('_id'))
print("status      :", found.get('status'))
print("scheduleAt  :", found.get('scheduleAt'), f"({found.get('scheduleAt')[:16]} UTC = 20:00 WIB)")
print("type/topic  :", found.get('type'), '/', found.get('topic'))
print("accountId   :", found.get('accountId'))
print("medias      :", [m.get('url') for m in (found.get('medias') or [])])
print("replies     :", len(found.get('replies') or []))
print("errorMessage:", found.get('errorMessage'))
print("caption len :", len(desc), "| paragraf:", len([p for p in desc.split('\n\n') if p.strip()]))
checks = {
    'media == file hari ini': [m.get('url') for m in (found.get('medias') or [])] == [IMG],
    'link artikel ada': URL in desc,
    'CTA diskusi': any(w in desc.lower() for w in ['komentar', 'diskusi', 'menurut', 'share', 'pendapat']),
    '0 mention': not re.search(r'@\w+', desc),
    'tanpa harga/biaya/murah/mahal/hemat': not any(w in desc.lower() for w in ['harga', 'biaya', 'murah', 'mahal', 'hemat']),
    '0 hashtag': not re.findall(r'#\w+', desc),
    'pending': found.get('status') == 'pending',
}
for k, v in checks.items():
    print(f"  [{'✅' if v else '❌'}] {k}")
print("pending hari ini:", pend_today)

hist = json.load(open('/home/mantapman/.openclaw/workspace-atun/post_das_fb_history.json'))
last = hist['posts'][-1]
print("history terakhir:", json.dumps(last, ensure_ascii=False))
print("total posts:", len(hist['posts']))
print("CAPTION:\n" + desc)
