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

API_KEY = "vio_sk_vA8llIYm51J3oyXtfevrM92yUBJ8-3IS1sIn6xydF8o"

print("🎨 Generating Scene 4 - CTA...")
payload = {
    "prompt": "Instagram Reels vertical image 9:16 portrait. Warm cream background. Text MULAI SEKARANG in deep teal bold at top center. Rounded white box with teal border containing healing icon and text Coba praktikkan di story kamu dan tag kami untuk join komunitas kesehatan mental yang lebih sadar in orange bold. At bottom balimentari handle deep teal bold. Below Mind Body Soul tagline with sun icon gray. Cute chibi character Surya orange sun at top with big happy smile thumbs up gesture and warm gentle eyes. 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 4 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())
