#!/usr/bin/env python3
"""Cek apakah sebuah slug/URL DAS sudah pernah dipakai di akun DAS FB (SEMUA field, semua status)."""
import json, sys, urllib.request
from repliz_client import HEADERS, BASE, ACC

needle = sys.argv[1]
hits = []
page = 1
total = 0
while True:
    req = urllib.request.Request(f'{BASE}/public/schedule?page={page}&limit=100', headers=HEADERS)
    data = json.loads(urllib.request.urlopen(req, timeout=60).read())
    for d in data.get('docs', []):
        total += 1
        if d.get('accountId') != ACC.DAS_FB:
            continue
        blob = json.dumps(d, ensure_ascii=False).lower()
        if needle.lower() in blob:
            hits.append((d.get('status'), d.get('scheduleAt'), d.get('_id'),
                         (d.get('description') or '')[:80].replace('\n', ' ')))
    if page >= data.get('totalPages', 1):
        break
    page += 1
print(f"total jadwal discan: {total}")
print(f"hit untuk '{needle}' di akun DAS FB: {len(hits)}")
for h in hits:
    print("  ", h)
