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

API_KEY = "vio_sk_vA8llIYm51J3oyXtfevrM92yUBJ8-3IS1sIn6xydF8o"

print("🎨 Generating Scene 3 - SCIENCE...")
payload = {
    "prompt": "Instagram Reels vertical image 9:16 portrait. Deep teal background. Text INI CARANYA in white bold at top center. Visual diagram showing: icon with text AKTIF followed by arrow pointing down, icon with text TENANG. Below text exhale lebih lama dari inhale mengirim sinyal ke otak bahwa kamu aman dan rileks. Cute chibi character Surya orange sun at bottom center with peaceful zen expression eyes half-closed. Clean modern minimalist wellness content. Professional design.",
    "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}")
        
        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 3 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())
