#!/usr/bin/env python3
"""PHAT Slide 14 - Built to Scale - v2 matching deck's butter/cream/champagne style."""

import base64
import json
import os
import sys
import urllib.request

API_KEY = os.environ.get("GEMINI_API_KEY")
model = "imagen-4.0-generate-001"
url = f"https://generativelanguage.googleapis.com/v1beta/models/{model}:predict?key={API_KEY}"

prompt = """Bright high-key editorial food photography. Cream and champagne color palette — butter yellow, pale gold, soft warm white. NOT sepia, NOT amber, NOT brown. The warmth comes from the subject catching golden-hour light, not from a color filter.

Subject: Interior of an industrial cultivation facility — large stainless steel bioreactor vessels with clean brushed surfaces reflecting warm champagne-gold light. The steel reads as bright, clean, and premium — like polished champagne gold, not tarnished bronze. Multiple vessels receding into background showing scale and depth. Premium craft distillery aesthetic — immaculate, minimal, refined.

Soft diffused directional light from upper left. High-key fill — shadows are gentle, lifted, warm-neutral (soft taupe, never deep brown). Background is bright cream to pale butter yellow. Clean whites remain readable as white, just slightly warm-tinted. Shallow depth of field — foreground vessel sharp, background softly blurred.

Vertical 3:4 composition, tight crop, 3/4 angle view. Quiet luxury. Editorial product photography style — the kind of lighting used for premium food brands. No text, no labels, no signage, no people. No dark zones. Bright throughout. No sepia filter. No heavy amber cast."""

payload = {
    "instances": [{"prompt": prompt}],
    "parameters": {
        "sampleCount": 4,
        "aspectRatio": "3:4",
        "personGeneration": "dont_allow"
    }
}

data = json.dumps(payload).encode()
req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"})

print("Generating PHAT Slide 14 v2 - bright cream/champagne style...")
try:
    with urllib.request.urlopen(req, timeout=180) as resp:
        result = json.loads(resp.read())
except urllib.error.HTTPError as e:
    print(f"HTTP {e.code}: {e.read().decode()[:500]}")
    sys.exit(1)

outdir = "/root/.openclaw/workspace/output/phat-slide14-scale-v2"
os.makedirs(outdir, exist_ok=True)

count = 0
for i, pred in enumerate(result.get("predictions", [])):
    if "bytesBase64Encoded" in pred:
        img_data = base64.b64decode(pred["bytesBase64Encoded"])
        out = f"{outdir}/scale-v{i+1}.png"
        with open(out, "wb") as f:
            f.write(img_data)
        print(f"Saved: {out} ({len(img_data):,} bytes)")
        count += 1

print(f"\nDone! {count} images generated.")
