#!/usr/bin/env python3
"""PHAT Slide 14 - Built to Scale - Cultivation facility in warm golden light."""

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 = """Warm golden food photography, bright abundant warm light flooding the scene like golden afternoon sun. Monochromatic warm palette — everything in gold, cream, amber family. Warm shadows cast in amber and brown tones, never cool. High luminosity, airy, lifted shadows. Clean warm background.

Subject: Industrial cultivation facility interior — large stainless steel bioreactor vessels and tanks bathed in warm golden afternoon light streaming through high windows. The scale is visible through the impressive size and depth of the vessels receding into the background. Premium craft distillery aesthetic — immaculately clean brushed steel surfaces catching and reflecting warm golden light. Subtle condensation on vessel surfaces adding tactile realism.

Tight hero framing with shallow depth of field. Foreground vessel sharp, background vessels softly blurred. Subtle warm sheen on all metal surfaces. Rich saturated warm yellow and amber tones throughout. Quiet luxury feeling, minimal, no decoration or clutter. Vertical 3:4 composition, tight crop. 3/4 angle view showing depth of the facility.

No cool tones, no blue light, no gray shadows, no dark zones. Warm throughout every part of the image. No text, no labels, no signage, no people. Pure industrial beauty in golden light."""

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 - Built to Scale (3:4 warm golden cultivation)...")
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"
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.")
