#!/usr/bin/env python3
"""Hero piloto 04 v2 — CURIOSIDADE — VERTICAL — v3.

Correcao: a v2 vertical ficou com a MESMA mulher jovem replicada em quase
todas as 16 celulas do grid. Prompt reforcado pra forcar diversidade real
(etnias, generos, idades, estilos). Testamos 3x3 (9 rostos) pra facilitar
a diferenciacao, se continuar replicando mudamos pra 4x3.

Saida: hero_curiosidade_v2_vertical.png (SOBRESCREVE a atual quando OK).
Durante iteracao, salva como hero_curiosidade_v2_vertical_tryN.png.
"""
import os
import sys
import base64
from pathlib import Path

env_path = Path("/opt/mia/.env")
for line in env_path.read_text().splitlines():
    if line.startswith("OPENAI_API_KEY="):
        os.environ["OPENAI_API_KEY"] = line.split("=", 1)[1].strip()
        break

try:
    from openai import OpenAI
except ImportError:
    os.system("pip install --break-system-packages openai -q")
    from openai import OpenAI

client = OpenAI()
OUT = Path("/opt/mia/workspace/clientes/px3lab/lancamento_photorf2/criativos_oferta")

# ---------- CONFIG ----------
TRY_LABEL = sys.argv[1] if len(sys.argv) > 1 else "try1"
GRID = sys.argv[2] if len(sys.argv) > 2 else "3x3"  # 3x3 (9) ou 4x3 (12) ou 4x4 (16)

grid_specs = {
    "3x3": ("a clear 3x3 grid of 9 portrait photos", "9 DIFFERENT people"),
    "4x3": ("a clear 4-columns-by-3-rows grid of 12 portrait photos", "12 DIFFERENT people"),
    "4x4": ("a clear 4x4 grid of 16 portrait photos", "16 DIFFERENT people"),
}
grid_desc, people_count = grid_specs[GRID]

prompt = (
    f"Photorealistic cinematic vertical portrait shot of an open modern MacBook laptop viewed from "
    f"above-front at a slight three-quarter angle. "
    f"The laptop screen prominently displays {grid_desc} arranged as a photo gallery in a photo "
    f"management application. "
    f"CRITICAL REQUIREMENT: each thumbnail shows a COMPLETELY DIFFERENT individual — {people_count}, "
    f"NO REPETITION whatsoever, NO duplicates, NO similar-looking faces. "
    f"The group is deliberately diverse: a MIX of ethnicities (Black, White, Latino/Brown, East Asian, "
    f"South Asian, Middle Eastern), a MIX of genders (men and women roughly balanced), a MIX of ages "
    f"from 18 to 45, a MIX of hairstyles (short, long, curly, straight, bald, braided, with glasses, "
    f"with beards), different face shapes (round, oval, square, long), different smiles (some wide, "
    f"some soft, some serious), different graduation gown colors (black, dark navy blue, deep maroon/burgundy, "
    f"forest green), different graduation caps or no cap, different ceremony backgrounds (some outdoor, "
    f"some auditorium, some stage). "
    f"Each portrait is clearly a distinct human being — if you look at any two thumbnails side by side "
    f"they must look like obviously different people. "
    f"Natural warm skin tones on each portrait, soft studio-like portrait lighting on each thumbnail, "
    f"typical professional event photography look. "
    f"The keyboard of the laptop is partially visible in the lower frame with soft key reflections. "
    f"The laptop sits on a dark wooden desk. "
    f"Cinematic soft dramatic side lighting from the upper left. "
    f"Very subtle lime-green glow (hex #87C925) coming only from a thin accent line on the screen edge, "
    f"discreet, not dominating. "
    f"Overall color palette is natural warm tones of the photos themselves. "
    f"A relaxed adult human hand rests completely still on the wooden desk to the right of the laptop, "
    f"palm down, fingers relaxed, NOT touching the keyboard, NOT clicking, just observing. "
    f"The hand has natural skin tone, lit by soft ambient light, visible and clearly identifiable as a "
    f"human hand. "
    f"Deep dark background with soft bokeh and subtle texture at the top and bottom of the frame, not "
    f"pure black. "
    f"Atmosphere: calm productive tech, mysterious but inviting, as if the computer is organizing the "
    f"photos by itself while the person just watches. "
    f"Shot on 50mm lens, shallow depth of field with sharp focus on the laptop screen showing the "
    f"photos clearly. "
    f"Editorial technology photography, hyper detailed, 8k, photorealistic. "
    f"No text, no readable logos on the screen, no watermarks, no visible laptop brand. "
    f"Vertical portrait 2:3 composition, extra dark space above and below the laptop for text overlay."
)

dest_try = OUT / f"hero_curiosidade_v2_vertical_{TRY_LABEL}_{GRID}.png"
print(f"Gerando hero vertical v3 [{TRY_LABEL} / grid {GRID}] via gpt-image-1...")
r = client.images.generate(
    model="gpt-image-1",
    prompt=prompt,
    size="1024x1536",
    quality="high",
    n=1,
)
b64 = r.data[0].b64_json
dest_try.write_bytes(base64.b64decode(b64))
print(f"OK -> {dest_try}")
