#!/usr/bin/env python3
"""One-shot read-only Suno feed reconciliation into a new dated archive batch."""
import datetime
import hashlib
import json
import sqlite3
import time
import urllib.request
from pathlib import Path

ROOT = Path('/home/vk/analytics/streamlit/radio/uploads/suno')
CUTOFF = '2026-09-27T13:53:00.066Z'
ENDPOINTS = [
    ('de', 'http://213.165.37.99:3000'),
    ('piter', 'http://45.150.39.199:3000'),
    ('fra', 'http://159.89.18.237:3000'),
    ('ams', 'http://167.172.40.19:3000'),
    ('ny', 'http://159.203.111.219:3000'),
]
ACCEPTED_EXCEPTIONS = {
    'dc58b0a0-2903-40b7-9287-2c3b9ba5df3b',
    '13a674a5-992b-4704-bc34-90ed216dfc93',
    'a7a9ab1f-c23d-4dd0-9ae6-0c2f759263c0',
}

def main():
    db = sqlite3.connect('file:' + str(ROOT / 'db/tracks.sqlite') + '?mode=ro', uri=True)
    known = {row[0]: row[1] for row in db.execute('SELECT id,file_path FROM tracks')}
    db.close()
    rows, pages, errors, seen = {}, [], [], set()
    boundary = False
    for page in range(200):
        data = source = None
        for name, base in ENDPOINTS:
            try:
                with urllib.request.urlopen(base + '/api/get?page=' + str(page), timeout=12) as response:
                    data = json.load(response)
                if not isinstance(data, list):
                    raise ValueError('API response is not a list')
                source = name
                break
            except Exception as exc:
                errors.append({'page': page, 'source': name, 'error': str(exc)[:160]})
        if data is None:
            break
        signature = hashlib.sha256(json.dumps([x.get('id') for x in data]).encode()).hexdigest()
        if data and signature in seen:
            errors.append({'page': page, 'error': 'repeated page signature'})
            break
        seen.add(signature)
        dates = [str(x.get('created_at') or '') for x in data]
        pages.append({'page': page, 'source': source, 'count': len(data),
                      'oldest': min(dates) if dates else None,
                      'newest': max(dates) if dates else None})
        for row in data:
            uid = row.get('id')
            if uid and str(row.get('created_at') or '') > CUTOFF:
                rows[uid] = row
        if not data or all(date <= CUTOFF for date in dates):
            boundary = True
            break
        time.sleep(0.2)
    if not boundary:
        raise RuntimeError('Feed did not reach the fixed historical boundary; no queue written')
    items, queue, local_ready_unbound = [], [], []
    for uid, row in rows.items():
        formats = [ext for ext in ('mp3', 'm4a') if (ROOT / 'audios' / (uid + '.' + ext)).is_file()]
        has_db = uid in known
        complete = row.get('status') == 'complete'
        accepted = uid in ACCEPTED_EXCEPTIONS
        items.append({'id': uid, 'title': row.get('title'), 'created_at': row.get('created_at'),
                      'status': row.get('status'), 'in_db': has_db,
                      'local_formats': formats, 'accepted_exception': accepted})
        if complete and not accepted:
            if not formats:
                queue.append(uid)
            elif not has_db:
                local_ready_unbound.append(uid)
    items.sort(key=lambda x: str(x['created_at']), reverse=True)
    queue.sort(key=lambda uid: str(rows[uid].get('created_at')), reverse=True)
    stamp = datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ')
    out = ROOT / 'imports' / ('discovery-all-fresh-' + stamp)
    out.mkdir(mode=0o700)
    report = {'scope': 'main Suno account, all clips newer than fixed 2026-09-27 boundary',
              'cutoff': CUTOFF, 'checked_at_utc': stamp, 'boundary_reached': boundary,
              'pages': pages, 'errors': errors, 'discovered': len(items),
              'complete': sum(x['status'] == 'complete' for x in items),
              'ready_db_audio': sum(x['status'] == 'complete' and x['in_db'] and bool(x['local_formats']) for x in items),
              'missing_audio_queue': len(queue), 'accepted_exceptions': sorted(ACCEPTED_EXCEPTIONS & set(rows)),
              'local_ready_unbound': local_ready_unbound,
              'newest': items[0] if items else None, 'items': items}
    metadata = {uid: row for uid, row in rows.items() if row.get('status') == 'complete'}
    for name, payload in [('discovery.json', report), ('full_metadata_correct.json', metadata)]:
        path = out / name
        path.write_text(json.dumps(payload, ensure_ascii=False, indent=2))
        path.chmod(0o600)
    path = out / 'decode-queue.txt'
    path.write_text('\n'.join(queue) + ('\n' if queue else ''))
    path.chmod(0o600)
    print(json.dumps({'manifest': str(out), 'pages': len(pages), 'boundary_reached': boundary,
                      'discovered': len(items), 'complete': report['complete'],
                      'ready_db_audio': report['ready_db_audio'], 'queue': len(queue),
                      'local_ready_unbound': len(local_ready_unbound),
                      'accepted_exceptions': report['accepted_exceptions'],
                      'newest_created_at': items[0]['created_at'] if items else None}, ensure_ascii=False))

if __name__ == '__main__':
    main()
