#!/usr/bin/env python3
"""Probe akun & jadwal Yasma Natura di Repliz."""
import json
from repliz_client import ReplizClient

c = ReplizClient()

# 1) daftar akun
accts = []
page = 1
while True:
    r = c.get('/public/account?page=%d&limit=100' % page)
    docs = r.get('docs') or r.get('data') or []
    accts.extend(docs)
    total = r.get('totalDocs') or 0
    if not docs or len(accts) >= total:
        break
    page += 1

print("TOTAL AKUN:", len(accts))
for a in accts:
    blob = json.dumps(a).lower()
    if 'asma' in blob:
        print("  YASMA AKUN:", a.get('_id'), '|', a.get('type'), '|', a.get('name'), '|', a.get('username'))

with open('/tmp/yasma_accounts.json', 'w') as f:
    json.dump(accts, f, indent=1)

# 2) semua jadwal
scheds = []
page = 1
while True:
    r = c.get_schedules(page=page, limit=100)
    docs = r.get('docs') or r.get('data') or []
    scheds.extend(docs)
    total = r.get('totalDocs') or 0
    if not docs or len(scheds) >= total:
        break
    page += 1
print("TOTAL JADWAL:", len(scheds))

YID = '6a3b41e3c5ff4ce3a327544b'
mine = [s for s in scheds if s.get('accountId') == YID]
print("JADWAL AKUN YASMA %s: %d" % (YID, len(mine)))
mine.sort(key=lambda s: s.get('scheduleAt') or '')
for s in mine:
    url = (s.get('medias') or [{}])[0].get('url', '') if s.get('medias') else ''
    print("  %s | %-6s | %-8s | %-45s | %s" % (
        s.get('scheduleAt'), s.get('type'), s.get('status'),
        (s.get('title') or '')[:45], url[:60]))
with open('/tmp/yasma_schedules.json', 'w') as f:
    json.dump(mine, f, indent=1)
