#!/usr/bin/env python3
"""Hero piloto 06 — ANGULO E — TERCEIRIZACAO — V2.

FIX 2026-10-08 (segunda tentativa):
- V1 cortou a cabeca no topo (sem headroom).
- V1 nao deixou espaco limpo no canto superior-direito pra logo.
Resultado: no criativo renderizado, logo ficou sobreposta na testa do cara.

V2 corrige com reforco de prompt:
- FULL HEAD AND HAIR VISIBLE with 15% HEADROOM at the top (nao cortar topo da cabeca).
- SUBJECT POSITIONED ON THE RIGHT (3/4 do frame cara/laptop) mas com AMPLA area limpa
  no canto SUPERIOR-DIREITO (parede/fundo escuro) pra logo ir sem conflito.
- Laptop a esquerda, cara ao centro-direita mas MAIS BAIXO no frame (dando headroom).
"""
import os
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")

prompt = (
    "Cinematic photorealistic three-quarter portrait of a self-employed Brazilian photographer in his 30s, "
    "seated at his wooden desk inside a small home photography studio, body turned three-quarters to the left, "
    "clearly looking DOWN AND TO THE LEFT at his open laptop screen, "
    "calm confident focused expression — professional satisfaction, NOT tired, NOT stressed, NOT euphoric, "
    "wearing a casual neutral dark t-shirt or henley, hands relaxed near the laptop keyboard, one hand lightly resting on the trackpad, "
    "FULL HEAD AND HAIR VISIBLE, with generous 15% empty HEADROOM space at the top of the frame above the head, "
    "do NOT crop the top of the head or the hair, the entire scalp and hair must be clearly visible with empty background behind and above, "
    "subject positioned slightly LOWER in the frame to leave the upper quarter of the image mostly as calm empty background, "
    "the modern laptop sits open on the wooden desk in the LEFT portion of the frame and prominently displays a visible grid of 16 real portrait photos arranged in a 4x4 gallery layout, "
    "the photos on screen show different young people wearing graduation gowns and formal attire smiling at a graduation ceremony, each thumbnail is clearly a recognizable human face photograph with natural warm skin tones, colored clothing, and real event backgrounds, "
    "the photos look like typical professional event photography thumbnails being auto-organized in a photo management app, "
    "NATURAL DIFFUSED DAYLIGHT streaming in from a window on the left side of the room, bright productive midday light, soft shadows, NOT golden hour, NOT sunset, this is clearly work in progress during the day, "
    "visible background behind the photographer: cozy indie home studio of a solo freelancer, a single DSLR camera resting on a shelf softly out of focus, one or two small framed photo prints on the warm-painted wall, a small plant, warm wood tones, "
    "the UPPER-RIGHT quadrant of the image must be a CLEAN QUIET DARK WALL area with no busy details — this empty space is intentional and reserved for a logo text overlay in post-production, "
    "ABSOLUTELY NO other people visible, NO second computer, NO extra monitors beyond the one laptop, NO stacks of external hard drives, NO printed contracts or outsourcing paperwork, NO office cubicles, NO corporate environment, NO team meeting background — the photographer is COMPLETELY ALONE in his small personal workspace, "
    "the narrative mood is QUIET CONTROL and OWNERSHIP — one person running his entire operation by himself, in command, no outsourcing needed, "
    "very subtle lime-green glow (hex #87C925) coming only from a thin accent line on the laptop screen edge, extremely discreet, barely noticeable, "
    "the dominant palette is natural warm daylight, wood tones and skin tones, "
    "shot on 35mm lens at f/2.8, wider framing to include FULL head with HEADROOM, sharp focus on the photographer's face in the right portion of the frame and the laptop screen in the left portion of the frame, both clearly in frame, "
    "editorial lifestyle technology photography, hyper detailed, 8k, photorealistic, "
    "NO clocks, NO digital numbers, NO watches visible, NO timestamps, NO text overlays, no readable logos on the screen, no watermarks, no visible laptop brand, "
    "square 1:1 composition, subject offset to the center-right side with empty upper-right dark wall area for text overlay, laptop prominent on the left side"
)

print("Gerando hero E V2 (com headroom + canto limpo pra logo) via gpt-image-1 (30-50s)...")
r = client.images.generate(
    model="gpt-image-1",
    prompt=prompt,
    size="1024x1024",
    quality="high",
    n=1,
)
b64 = r.data[0].b64_json
dest = OUT / "hero_terceirizacao_E_v2.png"
dest.write_bytes(base64.b64decode(b64))
print(f"OK -> {dest}")
