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

API_KEY = "vio_sk_vA8llIYm51J3oyXtfevrM92yUBJ8-3IS1sIn6xydF8o"

print("🎨 Generating Scene 1 - HOOK (v2 Bahasa Indonesia)...")
payload = {
    "prompt": "Instagram Reels vertical image 9:16 portrait. Deep teal background. Cute chibi character Surya anak matahari berwarna oranye keemasan di bagian atas layar dengan mata besar ekspresif menatap ke atas dengan rasa ingin tahu yang tulus, tangan terangkat dalam gestur bertanya dengan hangat. Text besar di tengah layar dalam box rounded orange: APA KAMU MENGALAMI STRES? OVERTHINGKING? KECEMASAN? COBA TEKNIK INI. SEKARANG. di bagian bawah dalam warna oranye. Bottom center handle @balimentari dalam putih. Desain modern minimalis untuk konten kesehatan mental dan psikologi yang 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}")
        
        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 1 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())
