#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
V3D do estatico C5 (conversa quarto pesado).

Correcao vs v3c/v3b/v3a:
- Renato reclamou 3x que o bloco "MINHA RESPOSTA" encostava no corpo
  (rosto/barba/queixo/braço) do Borrello.
- Analise pixel-a-pixel do PNG com alpha:
  * Foto original 1282x1600, corpo ocupa toda area
  * v3c: bloco em x=[650..1030], corpo ate x=830 na altura do bloco -> overlap real
- Solucao arquitetural v3d:
  * Foto REPOSICIONADA pro lado direito (right:-140px) e ampliada para
    ocupar coluna direita inteira (height 780px). Presenca do Borrello mantida.
  * Corpo dele agora comeca em x~595 (verificado por linha)
  * Bloco "MINHA RESPOSTA" movido pro EIXO da headline (left:60, width:500),
    ABAIXO da headline (top:830). Termina em x=560 -> 35px folga do corpo.
  * ZERO sobreposicao entre bloco e Borrello (verificado).
- Polegar apontando: substituida seta por indicativo grafico no proprio
  bloco (aspas + destaque amarelo), ja que geometria mudou.

Mantido:
- Headline esquerda com ESTA acentuado + hl PESADO
- Selo topo "CONVERSA . UM ALUNO ME PERGUNTOU"
- CTA amarelo footer
- Assinatura Francisco Borrello / @franciscoborrello
- Paleta preto + amarelo #F6E600
- Bebas Neue com acento
"""
import os
import base64
import tempfile
from pathlib import Path
from playwright.sync_api import sync_playwright

OUT = Path("/opt/mia/workspace/clientes/borrello/criativos_ig_teste_v3_2026-09-18/estaticos_v3")
OUT.mkdir(parents=True, exist_ok=True)

IMG_BASE = Path("/opt/mia/workspace/clientes/borrello/criativos_ig_teste_v3_2026-09-18/imagens_base")

W, H = 1080, 1350


def to_b64(path: Path) -> str:
    with open(path, "rb") as f:
        return base64.b64encode(f.read()).decode()


BORRELLO_B64 = to_b64(IMG_BASE / "borrello_c5_recorte.png")


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=Bebas+Neue&family=Inter:wght@400;500;600;700;800;900&display=swap" rel="stylesheet">
<style>
  * {{ margin: 0; padding: 0; box-sizing: border-box; }}
  html, body {{ margin: 0; padding: 0; overflow: hidden; width: {W}px; height: {H}px; font-family: 'Inter', sans-serif; }}
  .canvas {{ position: relative; width: {W}px; height: {H}px; overflow: hidden; }}

  .pill {{
    display: inline-flex; align-items: center; gap: 10px;
    border: 2px solid #F6E600; border-radius: 100px;
    padding: 12px 26px;
    font-family: 'Inter', sans-serif; font-weight: 800; font-size: 22px;
    letter-spacing: 2px; color: #FFF;
    background: rgba(0,0,0,0.55);
    backdrop-filter: blur(6px);
  }}
  .pill.dark {{ background: #000; color: #FFF; }}

  .h1 {{
    font-family: 'Bebas Neue', 'Oswald', sans-serif;
    font-weight: 400;
    text-transform: uppercase;
    line-height: 0.98;
    letter-spacing: 0px;
    color: #FFF;
    text-shadow: 0 4px 24px rgba(0,0,0,0.75);
  }}

  .hl {{
    background: #F6E600;
    color: #111;
    padding: 2px 18px 8px 18px;
    border-radius: 8px;
    display: inline-block;
    line-height: 0.95;
    margin: 4px 0;
    text-shadow: none;
  }}

  .dash {{
    display: block; width: 60px; height: 4px; background: #F6E600;
    margin-bottom: 22px;
  }}

  .ig-bar {{
    position: absolute;
    left: 40px; right: 40px;
    bottom: 130px;
    display: flex; align-items: center; gap: 14px;
    z-index: 10;
  }}
  .ig-bar .avatar {{
    width: 56px; height: 56px; border-radius: 50%;
    background: linear-gradient(135deg, #F6E600, #B8860B);
    overflow: hidden;
    border: 2px solid rgba(255,255,255,0.15);
  }}
  .ig-bar .name {{ display: flex; flex-direction: column; }}
  .ig-bar .name b {{ font-family: 'Inter'; font-weight: 700; font-size: 22px; color: #FFF; text-shadow: 0 2px 10px rgba(0,0,0,0.7); }}
  .ig-bar .name span {{ font-family: 'Inter'; font-weight: 400; font-size: 18px; opacity: 0.75; margin-top: 2px; color: #FFF; text-shadow: 0 2px 10px rgba(0,0,0,0.7); }}
  .ig-bar .btn {{
    padding: 10px 16px; border-radius: 8px;
    font-family: 'Inter'; font-size: 16px; font-weight: 500;
    background: rgba(255,255,255,0.18); color: #FFF; backdrop-filter: blur(4px);
  }}
  .ig-bar .dots {{ font-size: 22px; margin-left: auto; opacity: 0.9; color: #FFF; }}

  .cta {{
    position: absolute; left: 40px; right: 40px; bottom: 40px;
    background: #F6E600; color: #000;
    text-align: center;
    padding: 22px 20px; border-radius: 12px;
    font-family: 'Inter'; font-weight: 900; font-size: 26px;
    text-transform: uppercase; letter-spacing: 1px;
    z-index: 10;
    box-shadow: 0 12px 40px rgba(246,230,0,0.35);
  }}

  .dots-grid {{
    position: absolute;
    background-image: radial-gradient(#F6E600 2px, transparent 2px);
    background-size: 12px 12px;
    opacity: 0.55;
  }}

  /* V3D: bloco no eixo da headline (esquerda), abaixo dela.
     Foto do Borrello ocupa coluna direita com right:-140, comeca em x=595.
     Bloco: left=60, width=500 -> termina em x=560, folga 35px.
     Vertical: headline base ~y=810, ig-bar top ~y=1170.
     Bloco: top=850 (abaixo headline), altura ~200 -> base ~1050 (acima ig-bar). */
  .resposta-box {{
    position: absolute;
    top: 850px;
    left: 60px;
    width: 500px;
    padding: 22px 26px 26px 26px;
    background: rgba(0,0,0,0.82);
    backdrop-filter: blur(10px);
    border: 2px solid rgba(246,230,0,0.6);
    border-radius: 14px;
    z-index: 8;
    box-shadow: 0 20px 60px rgba(0,0,0,0.7);
  }}
  .resposta-box .label {{
    font-family: 'Inter'; font-size: 20px; font-weight: 700; color: #F6E600;
    letter-spacing: 3px;
    margin-bottom: 12px;
    display: block;
  }}
  .resposta-box .texto {{
    font-family: 'Inter'; font-size: 26px; font-weight: 500; color: #FFF;
    line-height: 1.28;
  }}
  .resposta-box .texto b {{
    color: #F6E600; font-weight: 700;
  }}
</style>
</head><body>
<div class="canvas" style="background:
  radial-gradient(ellipse at 70% 60%, #1c1a12 0%, #000 65%);">

  <!-- Halo suave direita atras do Borrello -->
  <div style="position:absolute; right:-100px; bottom:180px;
              width:700px; height:700px;
              background: radial-gradient(circle, rgba(246,230,0,0.26) 0%, transparent 55%);
              border-radius:50%;"></div>

  <!-- Foto REAL do Borrello - coluna direita, height 780, right -140 -->
  <img src="data:image/png;base64,{BORRELLO_B64}"
       style="position:absolute; right:-140px; bottom:180px;
              height: 780px; width: auto;
              filter: drop-shadow(0 20px 40px rgba(0,0,0,0.7));" />

  <!-- Textura pontos superior direita -->
  <div class="dots-grid" style="right:60px; top:100px; width:120px; height:80px;"></div>

  <!-- Pill topo esquerda -->
  <div style="position:absolute; top:70px; left:60px; z-index:5;">
    <div class="pill dark">CONVERSA <span style="color:#F6E600">.</span> UM ALUNO ME PERGUNTOU</div>
  </div>

  <!-- Aspas gigantes decorativas -->
  <div style="position:absolute; top:150px; left:60px;
              font-family:'Bebas Neue'; font-size:200px; color:#F6E600;
              line-height:0.7; opacity:0.88; z-index:4;">&ldquo;</div>

  <!-- Headline (esquerda) -->
  <div style="position:absolute; top:300px; left:60px; width:520px; z-index:5;">
    <span class="dash"></span>
    <h1 class="h1" style="font-size: 88px;">
      MEU<br>
      QUARTO<br>
      EST&Aacute;<br>
      <span class="hl">PESADO</span>.<br>
      E AGORA?
    </h1>
  </div>

  <!-- Bloco MINHA RESPOSTA - eixo esquerda, abaixo da headline, SEM tocar Borrello -->
  <div class="resposta-box">
    <span class="label">MINHA RESPOSTA:</span>
    <p class="texto">
      Abre a janela. Tira o que n&atilde;o te serve.
      E olha o que tem <b>atr&aacute;s da cabeceira</b>.
    </p>
  </div>

  <div class="ig-bar">
    <div class="avatar"></div>
    <div class="name"><b>Francisco Borrello</b><span>@franciscoborrello</span></div>
    <div class="btn">Seguindo v</div>
    <div class="btn">Enviar mensagem</div>
    <div class="dots">...</div>
  </div>

  <div class="cta">MANDA A SUA PERGUNTA NOS COMENT&Aacute;RIOS</div>
</div>
</body></html>
"""


def render(html, dest):
    tmp = tempfile.NamedTemporaryFile(mode="w", suffix=".html", delete=False, encoding="utf-8")
    tmp.write(html)
    tmp.close()
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page(viewport={"width": W, "height": H}, device_scale_factor=1)
        page.goto("file://" + tmp.name, wait_until="networkidle")
        page.wait_for_timeout(1200)
        page.screenshot(path=dest, clip={"x": 0, "y": 0, "width": W, "height": H})
        browser.close()
    os.unlink(tmp.name)
    print(f"render OK -> {dest}")


if __name__ == "__main__":
    render(HTML, str(OUT / "05_conversa_quarto_pesado_v3d.png"))
