#!/usr/bin/env python3
"""
Criativo piloto 02 Cloud PhotoRF 2.0 — Feed 1:1 (1080x1080)
Mensagem: "OLHO FECHADO? FORA."
Público: FRIO (aquisição) — donos de empresa de fotografia escolar/eventos
Autor: Juliana (juliana-ops)
Foto base: gpt-image-2.5-sunburst
"""

import base64
from pathlib import Path
from playwright.sync_api import sync_playwright

BASE = Path(__file__).resolve().parent
OUT = BASE / "piloto_02_olho_fechado.png"

ASSET_FOTO = BASE / "foto_base_olho_fechado_raw.png"
ASSET_LOGO = Path("/opt/mia/workspace/clientes/px3lab/landing_lancamento_novo/preview-lp/assets/logo-wordmark.png")


def b64_img(path: Path) -> str:
    data = path.read_bytes()
    ext = path.suffix.lstrip(".").lower()
    if ext == "jpg":
        ext = "jpeg"
    return f"data:image/{ext};base64," + base64.b64encode(data).decode()


FOTO_B64 = b64_img(ASSET_FOTO)
LOGO_B64 = b64_img(ASSET_LOGO)


HTML = f"""<!doctype html>
<html lang="pt-BR">
<head>
<meta charset="utf-8">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Barlow+Condensed:wght@700;800;900&family=Montserrat:wght@600;800;900&family=JetBrains+Mono:wght@500;700&display=swap" rel="stylesheet">
<style>
  * {{ box-sizing: border-box; margin: 0; padding: 0; }}
  html, body {{ width: 1080px; height: 1080px; overflow: hidden; background: #0a0a0c; font-family: 'Montserrat', sans-serif; -webkit-font-smoothing: antialiased; }}

  .canvas {{
    position: relative;
    width: 1080px;
    height: 1080px;
    background:
      radial-gradient(circle at 72% 42%, rgba(135,201,37,0.08) 0%, rgba(10,10,12,0) 55%),
      linear-gradient(180deg, #0f0f12 0%, #0a0a0c 100%);
    overflow: hidden;
  }}

  /* grid tech sutil ao fundo */
  .grid-bg {{
    position: absolute; inset: 0;
    background-image:
      linear-gradient(rgba(135,201,37,0.045) 1px, transparent 1px),
      linear-gradient(90deg, rgba(135,201,37,0.045) 1px, transparent 1px);
    background-size: 60px 60px;
    mask-image: radial-gradient(ellipse 70% 60% at 50% 50%, #000 40%, transparent 100%);
    -webkit-mask-image: radial-gradient(ellipse 70% 60% at 50% 50%, #000 40%, transparent 100%);
  }}

  /* HEADER: selo de campanha + wordmark */
  .header {{
    position: absolute;
    top: 56px; left: 60px; right: 60px;
    display: flex; align-items: center; justify-content: space-between;
    z-index: 10;
  }}
  .selo-campanha {{
    font-family: 'Barlow Condensed', sans-serif;
    font-weight: 900;
    font-size: 22px;
    letter-spacing: 0.14em;
    color: #ffffff;
    text-transform: uppercase;
    display: flex; align-items: center; gap: 10px;
  }}
  .selo-campanha .absurdo {{ color: #A6E22E; }}
  .selo-campanha .bar {{
    display: inline-block;
    width: 4px; height: 22px;
    background: #A6E22E;
    transform: skewX(-18deg);
    margin-right: 4px;
  }}
  .wordmark {{ height: 32px; opacity: 0.95; }}

  /* STAGE central: split foto + texto */
  .stage {{
    position: absolute;
    top: 150px; bottom: 220px; left: 60px; right: 60px;
    display: grid;
    grid-template-columns: 1fr 440px;
    gap: 40px;
    align-items: center;
  }}

  .copy {{
    display: flex; flex-direction: column; justify-content: center;
    height: 100%;
  }}
  .kicker {{
    font-family: 'JetBrains Mono', monospace;
    font-weight: 500;
    font-size: 18px;
    letter-spacing: 0.18em;
    color: #A6E22E;
    margin-bottom: 24px;
    text-transform: uppercase;
    display: flex; align-items: center; gap: 12px;
  }}
  .kicker::before {{
    content: "";
    display: inline-block; width: 36px; height: 2px; background: #A6E22E;
  }}

  .headline {{
    font-family: 'Barlow Condensed', sans-serif;
    font-weight: 900;
    font-size: 150px;
    line-height: 0.90;
    letter-spacing: -0.02em;
    color: #ffffff;
    text-transform: uppercase;
    margin-bottom: 28px;
  }}
  .headline .highlight {{
    color: #A6E22E;
    display: inline-block;
    position: relative;
  }}
  .headline .q {{ color: #A6E22E; }}

  .sub {{
    font-family: 'Montserrat', sans-serif;
    font-weight: 600;
    font-size: 24px;
    line-height: 1.4;
    color: #d6d6d6;
    max-width: 520px;
  }}
  .sub strong {{ color: #ffffff; font-weight: 800; }}

  /* FOTO LADO DIREITO */
  .photo-wrap {{
    position: relative;
    width: 440px; height: 580px;
    border-radius: 14px;
    overflow: hidden;
    background: #000;
    box-shadow: 0 30px 60px rgba(0,0,0,0.5), 0 0 0 1px rgba(255,68,68,0.25);
  }}
  .photo-wrap img.foto {{
    position: absolute; inset: 0;
    width: 100%; height: 100%;
    object-fit: cover;
    object-position: 50% 30%;
    filter: contrast(1.05) saturate(1.05);
  }}
  .photo-wrap::after {{
    /* overlay fundir com preto na base */
    content: "";
    position: absolute; inset: 0;
    background: linear-gradient(180deg, rgba(10,10,12,0) 55%, rgba(10,10,12,0.85) 100%);
  }}

  /* Brackets VERMELHOS nos cantos (rejeição) */
  .brk {{
    position: absolute;
    width: 54px; height: 54px;
    border: 5px solid #ff3b3b;
    z-index: 3;
    filter: drop-shadow(0 0 8px rgba(255,59,59,0.45));
  }}
  .brk.tl {{ top: 24px; left: 24px; border-right: none; border-bottom: none; }}
  .brk.tr {{ top: 24px; right: 24px; border-left: none; border-bottom: none; }}
  .brk.bl {{ bottom: 24px; left: 24px; border-right: none; border-top: none; }}
  .brk.br {{ bottom: 24px; right: 24px; border-left: none; border-top: none; }}

  /* X vermelho sobreposto (rejeitada) */
  .x-mark {{
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    width: 180px; height: 180px;
    z-index: 4;
    opacity: 0.85;
  }}
  .x-mark::before, .x-mark::after {{
    content: "";
    position: absolute;
    top: 50%; left: 0;
    width: 100%; height: 10px;
    background: #ff3b3b;
    border-radius: 4px;
    box-shadow: 0 0 20px rgba(255,59,59,0.6);
  }}
  .x-mark::before {{ transform: translateY(-50%) rotate(45deg); }}
  .x-mark::after {{ transform: translateY(-50%) rotate(-45deg); }}

  /* Pill de status REJEITADA sobre a foto */
  .status-pill {{
    position: absolute;
    bottom: 90px; left: 24px;
    background: rgba(10,10,12,0.78);
    backdrop-filter: blur(6px);
    border: 1px solid rgba(255,59,59,0.5);
    border-radius: 999px;
    padding: 10px 18px;
    display: flex; align-items: center; gap: 10px;
    font-family: 'JetBrains Mono', monospace;
    font-weight: 700;
    font-size: 13px;
    color: #ff5555;
    letter-spacing: 0.08em;
    z-index: 5;
  }}
  .status-pill .dot {{
    width: 8px; height: 8px; background: #ff3b3b;
    border-radius: 50%;
    box-shadow: 0 0 10px #ff3b3b;
  }}

  /* Tag de ID sobre a foto */
  .launch-tag {{
    position: absolute;
    top: 24px; right: 90px;
    font-family: 'JetBrains Mono', monospace;
    font-weight: 700;
    font-size: 12px;
    color: #ffffff;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    background: rgba(0,0,0,0.6);
    padding: 6px 10px;
    border-radius: 4px;
    z-index: 4;
  }}

  /* RODAPÉ: linha com CTA + mini-info */
  .footer {{
    position: absolute;
    left: 60px; right: 60px; bottom: 60px;
    display: flex; align-items: center; justify-content: space-between;
    z-index: 10;
  }}
  .cta {{
    background: #A6E22E;
    color: #0a0a0c;
    font-family: 'Montserrat', sans-serif;
    font-weight: 900;
    font-size: 24px;
    letter-spacing: 0.04em;
    padding: 20px 36px;
    border-radius: 10px;
    text-transform: uppercase;
    display: inline-flex; align-items: center; gap: 14px;
    box-shadow: 0 10px 30px rgba(166,226,46,0.25);
  }}
  .cta .arrow {{ font-size: 28px; line-height: 1; }}

  .meta-line {{
    display: flex; flex-direction: column; align-items: flex-end; gap: 6px;
  }}
  .meta-line .label {{
    font-family: 'JetBrains Mono', monospace;
    font-weight: 500;
    font-size: 12px;
    color: #8a8a90;
    letter-spacing: 0.18em;
    text-transform: uppercase;
  }}
  .meta-line .date {{
    font-family: 'Barlow Condensed', sans-serif;
    font-weight: 900;
    font-size: 44px;
    color: #ffffff;
    letter-spacing: 0.02em;
    line-height: 1;
  }}
  .meta-line .date .accent {{ color: #A6E22E; }}

  .meta-line .bar2 {{
    display: inline-block;
    width: 3px; height: 36px; background: #A6E22E;
    transform: skewX(-14deg);
    margin-right: 10px;
    vertical-align: middle;
  }}
</style>
</head>
<body>
<div class="canvas">
  <div class="grid-bg"></div>

  <!-- HEADER -->
  <div class="header">
    <div class="selo-campanha">
      <span class="bar"></span>
      PREPARE-SE PARA O <span class="absurdo">ABSURDO!</span>
    </div>
    <img class="wordmark" src="{LOGO_B64}" alt="Cloud PhotoRF 2.0">
  </div>

  <!-- STAGE -->
  <div class="stage">
    <div class="copy">
      <div class="kicker">Qualidade automática · reinventada</div>
      <h1 class="headline">
        OLHO<br>
        FECHADO<span class="q">?</span><br>
        <span class="highlight">FORA.</span>
      </h1>
      <p class="sub">
        O 2.0 filtra <strong>olho fechado</strong>, <strong>foco ruim</strong><br>
        e <strong>sorriso falso</strong> antes de você começar.
      </p>
    </div>

    <div class="photo-wrap">
      <img class="foto" src="{FOTO_B64}" alt="">
      <div class="brk tl"></div>
      <div class="brk tr"></div>
      <div class="brk bl"></div>
      <div class="brk br"></div>
      <div class="x-mark"></div>
      <div class="launch-tag">ID · 02 · REJEITADA</div>
      <div class="status-pill"><span class="dot"></span>OLHO FECHADO · FORA</div>
    </div>
  </div>

  <!-- FOOTER -->
  <div class="footer">
    <div class="cta">
      QUERO VER ANTES
      <span class="arrow">→</span>
    </div>
    <div class="meta-line">
      <span class="label">Lançamento</span>
      <span class="date"><span class="bar2"></span>07<span class="accent">.</span>10<span class="accent">.</span>26</span>
    </div>
  </div>
</div>
</body>
</html>
"""


def render():
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page(viewport={"width": 1080, "height": 1080}, device_scale_factor=2)
        page.set_content(HTML, wait_until="networkidle")
        page.wait_for_timeout(400)
        page.screenshot(path=str(OUT), clip={"x": 0, "y": 0, "width": 1080, "height": 1080}, omit_background=False)
        browser.close()
    from PIL import Image
    img = Image.open(OUT)
    if img.size != (1080, 1080):
        img = img.resize((1080, 1080), Image.LANCZOS)
        img.save(OUT, "PNG", optimize=True)
    print(f"[OK] Gerado: {OUT} ({OUT.stat().st_size // 1024} KB, {img.size})")
    return OUT


if __name__ == "__main__":
    render()
