import urllib.request, json, time, sys

BASE = 'http://127.0.0.1:18766'
RUN_ID = 2  # the upload we just did

# Step 1: Trigger manual analysis (full pipeline: clustering + UMAP + SVD)
print('[1] Starting manual analysis pipeline...')
body = json.dumps({
    'run_id': int(RUN_ID),
    'algorithm': 'hdbscan',
    'min_cluster_size': 5,
    'cluster_mode': 'raw',
}).encode('utf-8')

req = urllib.request.Request(f'{BASE}/analyze/run/', data=body,
    headers={'Content-Type': 'application/json'})
try:
    r = urllib.request.urlopen(req, timeout=10)
    resp = json.loads(r.read())
    r.close()
    print(f'[1] Started: {resp}')
except Exception as e:
    print(f'[1] FAILED: {e}')
    sys.exit(1)

# Step 2: Poll until completed or failed
print('[2] Polling analysis progress...')
for i in range(240):
    try:
        r = urllib.request.urlopen(f'{BASE}/runs/{RUN_ID}/status/', timeout=5)
        s = json.loads(r.read())
        r.close()
    except:
        time.sleep(2)
        continue

    status = s['status']
    pct = s.get('progress_pct', 0)
    msg = s.get('progress_msg', '')
    if i % 10 == 0:
        clusters = s.get('cluster_count') or '?'
        entities = s.get('entity_count') or '?'
        print(f'  [{i}] {status} ({pct}%) clusters={clusters} entities={entities} {msg[:60]}')

    if status == 'completed':
        print(f'\n[2] DONE!')
        print(f'    status: {status}')
        print(f'    cluster_count: {s.get("cluster_count")}')
        print(f'    entity_count: {s.get("entity_count")}')
        print(f'    progress: {pct}%')
        break
    if status == 'failed':
        err = s.get('error_message', '')[:800]
        print(f'\n[2] FAILED: {err}')
        break
    time.sleep(3)
else:
    print('[2] TIMEOUT after 12 minutes')

# Step 3: Verify cluster overview page
print('[3] Checking cluster overview...')
try:
    r = urllib.request.urlopen(f'{BASE}/clusters/{RUN_ID}/', timeout=10)
    body = r.read().decode('utf-8', errors='replace')
    r.close()
    has_scatter = 'scatter_data' in body
    has_geo = 'geo_data' in body
    has_umap = 'UMAP' in body
    print(f'    scatter_data: {has_scatter}')
    print(f'    geo_data: {has_geo}')
    print(f'    UMAP text: {has_umap}')
except Exception as e:
    print(f'    FAILED: {e}')
