#!/usr/bin/env python3
"""Generate a Mockup.Maison Low Light Collection style mockup."""

import json
import base64
import sys
import urllib.request
import urllib.error
from pathlib import Path

API_KEY = "AIzaSyDXqYZInk83iVV4mD29pSuHQKbgkiI1x9Q"
MODEL = "gemini-2.5-flash-image"

SCREEN_IMG = "/root/.openclaw/workspace/work/pitches/brandwatch/visuals/production-assets-screen.png"

PROMPT = """Place the provided screenshot onto a MacBook Pro laptop screen in a dark, low-light photography setting.

The MacBook Pro sits on a dark matte desk. The environment is very dark — the bright laptop screen is the primary light source, casting a warm glow onto the desk surface. A hand reaches toward the trackpad from the right side, softly illuminated by the screen light. The keyboard glows dimly from the screen backlight.

Shot from above at roughly 30 degrees, slightly off-center. 16:9 aspect ratio. The screen content from the input image must remain sharp, readable, and faithfully reproduced. Everything beyond the laptop fades to near-black. Photorealistic product photography quality, similar to premium device mockup photography."""

def load_image(path):
    data = Path(path).read_bytes()
    mime = "image/png" if path.endswith(".png") else "image/jpeg"
    return base64.b64encode(data).decode("utf-8"), mime

screen_b64, screen_mime = load_image(SCREEN_IMG)

url = f"https://generativelanguage.googleapis.com/v1beta/models/{MODEL}:generateContent?key={API_KEY}"

payload = {
    "contents": [{
        "parts": [
            {"text": PROMPT},
            {"inlineData": {"mimeType": screen_mime, "data": screen_b64}}
        ]
    }],
    "generationConfig": {
        "responseModalities": ["TEXT", "IMAGE"]
    }
}

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

print("Generating Low Light mockup...")
try:
    with urllib.request.urlopen(req, timeout=180) as resp:
        result = json.loads(resp.read().decode("utf-8"))
except urllib.error.HTTPError as e:
    body = e.read().decode("utf-8")
    print(f"HTTP {e.code}: {body[:500]}")
    sys.exit(1)

count = 0
for candidate in result.get("candidates", []):
    for part in candidate.get("content", {}).get("parts", []):
        if "inlineData" in part:
            count += 1
            suffix = f"-{count}" if count > 1 else ""
            output = f"/root/.openclaw/workspace/work/pitches/brandwatch/visuals/production-lowlight{suffix}.png"
            img_data = base64.b64decode(part["inlineData"]["data"])
            with open(output, "wb") as f:
                f.write(img_data)
            print(f"Saved: {output} ({len(img_data)} bytes)")
        elif "text" in part:
            print(f"Text: {part['text'][:200]}")

if count == 0:
    print("No image in response")
    print(json.dumps(result, indent=2)[:1000])
