import json, urllib.request, urllib.error, time

API_KEY = "vio_sk_vA8llIYm51J3oyXtfevrM92yUBJ8-3IS1sIn6xydF8o"

# Scene 2 - DEMO BOX BREATHING
print("🎨 Generating Scene 2 - DEMO BOX BREATHING...")
payload = {
    "prompt": "Instagram Reels vertical image 9:16 portrait. Lingkaran putih bercahaya di tengah layar yang berkembang dan menguncup seperti napas. Karakter chibi Surya anak matahari berwarna oranye di bawah lingkaran dengan ekspresi tenang mengikuti ritme napas. Background gradient biru teal dalam ke biru teal cerah. Text putih bold: TARIK 4 detik TAHAN 4 detik BUANG 8 detik. Text oranye bold: TEKNIK NAPAS. Desain modern minimalis konten kesehatan mental. Profesional dan menenangkan.",
    "model": "gpt-image-2.0",
    "aspect_ratio": "9:16",
    "count": 1
}

req = urllib.request.Request(
    'https://api.viostudio.id/v1/images/generate',
    data=json.dumps(payload).encode(),
    headers={
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {API_KEY}',
        'User-Agent': 'Mozilla/5.0'
    },
    method='POST'
)

try:
    with urllib.request.urlopen(req, timeout=30) as resp:
        result = json.loads(resp.read())
        gen_id = result.get('generation_ids', [0])[0]
        print(f"✅ Queued! ID: {gen_id}")
        
        # Poll for result
        for i in range(20):
            time.sleep(5)
            status_req = urllib.request.Request(
                f'https://api.viostudio.id/v1/generations/{gen_id}',
                headers={'Authorization': f'Bearer {API_KEY}', 'User-Agent': 'Mozilla/5.0'}
            )
            with urllib.request.urlopen(status_req, timeout=15) as sr:
                status = json.loads(sr.read())
                if status.get('status') == 'completed':
                    print(f"✅ Scene 2 DONE! URL: {status.get('asset_url')}")
                    break
                elif status.get('status') == 'failed':
                    print(f"❌ Failed: {status.get('error_message')}")
                    break
                print(f"   Processing... ({i+1}/20)")
except urllib.error.HTTPError as e:
    print('HTTP Error:', e.code, e.read().decode())
