import json, urllib.request, time

API_KEY = "vio_sk_vA8llIYm51J3oyXtfevrM92yUBJ8-3IS1sIn6xydF8o"

print("🎨 Generating Welcome Card - Sage Green...")

payload = {
    "prompt": "Instagram story vertical image 9:16 portrait. Flat vector minimalist design. Sage green and soft cream white color palette with gold accents. Clean elegant layout. Top section: small lotus flower icon and text RESILIENCE SPACE in elegant serif font deep sage green. Below: HAI KAK TERIMA KASIH YA in warm cream color. Main body white rounded card in center with soft shadow containing neatly arranged icons and text: a mirror icon with text BUKAN RAMALAN INI CERMIN, a chat bubble icon with text DM @resiliencespace.id, a clock icon with text SESI 20 MENIT VIA DM, and two time options with sun icons for PAGI 09.00-11.00 and SORE 16.00-20.00. Bottom section: small delicate tropical leaf illustrations, text DENGAN HANGAT RESI and LIAN with a small heart. Clean modern Indonesian wellness aesthetic, calming peaceful vibe, professional.",
    "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'
)

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(8)
        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())
            s = status.get('status')
            print(f"  ({i+1}/20) {s}")
            if s == 'completed':
                url = status.get('asset_url')
                print(f"  ✅ DONE! URL: {url}")
                break
            elif s == 'failed':
                print(f"  ❌ FAILED: {status.get('error_message')}")
                break
