#!/bin/bash

# === Prudential Storyboard - Image Generation ===
# Model: nano-banana-pro (3 credits per image)

API_KEY="vio_sk_vA8llIYm51J3oyXtfevrM92yUBJ8-3IS1sIn6xydF8o"
BASE_URL="https://api.viostudio.id/v1"

echo "🎬 Prudential Storyboard - Image Generation"
echo "============================================"

# Frame 1: The Whisper
echo ""
echo "📸 Generating Frame 1: The Whisper..."
curl -s -X POST "${BASE_URL}/images/generations" \
  -H "Authorization: Bearer ${API_KEY}" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "nano-banana-pro",
    "prompt": "Cinematic close-up. A father sits at a wooden dinner table, warm amber lighting at night. A small child, 6 years old, approaches quietly and whispers into his fathers ear. The father pauses and listens intently, gentle warm smile. Shallow depth of field, film grain, ARRI Alexa style. Emotional, intimate mood. 4K.",
    "aspect_ratio": "9:16",
    "seed": 1001
  }' | python3 -c "
import sys, json, time, requests
data = json.load(sys.stdin)
if 'data' in data:
    img = data['data'][0]
    gen_id = img.get('generation_id','')
    print(f'   Generation ID: {gen_id}')
    # Poll for result
    for i in range(30):
        time.sleep(5)
        r = requests.get(f'https://api.viostudio.id/v1/images/generations/{gen_id}', 
                        headers={'Authorization': 'Bearer $API_KEY'})
        result = r.json()
        if result.get('status') == 'completed':
            url = result['data'][0]['url']
            print(f'   ✅ Done! URL: {url}')
            # Download
            img_data = requests.get(url).content
            with open('/app/working/workspaces/default/prudential-frame1-whisper.jpg', 'wb') as f:
                f.write(img_data)
            print('   💾 Saved: prudential-frame1-whisper.jpg')
            break
        elif result.get('status') == 'failed':
            print(f'   ❌ Failed: {result}')
            break
        else:
            print(f'   ⏳ Waiting... ({i+1}/30)')
elif 'error' in data:
    print(f'   ❌ Error: {data[\"error\"]}')
"
