#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Fix cirúrgico slide 04 tartaruga — QA (OCR tesseract) leu "ATRAS À" no lugar de "ATRÁS,"
porque o acento da hero saiu com kerning que o OCR não conseguiu ligar à letra.

Correções aplicadas SÓ neste slide:
  1. line-height da hero aumentado (1.02 -> 1.10) pra dar respiro ao acento
  2. .hl com padding-top maior (6px -> 18px) e line-height 1.0 pra o "Á" caber
  3. Vírgula deslocada com espaço não-quebrável antes
  4. font-feature-settings pra Bebas Neue renderizar glifos limpos
"""
import os
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/carrossel_v3_com_acentos")
W, H = 1080, 1350


HEAD = 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&family=Noto+Serif+SC:wght@400;700;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: 12px;
    border: 3px solid #F6E600; border-radius: 100px;
    padding: 16px 32px; background: #000;
    font-family: 'Inter', sans-serif; font-weight: 800; font-size: 32px;
    letter-spacing: 2px; color: #FFF;
  }}
  /* HEADLINE — Bebas Neue com respiro maior pro acento */
  .h1 {{
    font-family: 'Bebas Neue', sans-serif; font-weight: 400;
    text-transform: uppercase; line-height: 1.14;   /* +respiro */
    letter-spacing: 1px; color: #FFF;
    font-feature-settings: "kern" 1, "liga" 1;
  }}
  .hl {{
    background: #F6E600; color: #111;
    padding: 18px 26px 14px 26px;    /* +padding topo pro Á caber */
    border-radius: 10px;
    display: inline-block; line-height: 1.0;
    margin: 8px 0;
  }}
  .dash {{
    display: block; width: 100px; height: 8px; background: #F6E600;
    margin-bottom: 26px;
  }}
  .swipe {{
    position: absolute; right: 40px; bottom: 36px;
    display: flex; align-items: center; gap: 16px;
    font-family: 'Inter'; font-weight: 800; font-size: 34px;
    color: #F6E600; letter-spacing: 3px;
    text-transform: uppercase;
    z-index: 10;
  }}
  .swipe .arrow {{
    display: inline-flex; align-items: center; justify-content: center;
    width: 78px; height: 78px; border-radius: 50%;
    background: #F6E600; color: #000; font-size: 44px; font-weight: 900;
  }}
  .footer {{
    position: absolute; left: 40px; bottom: 46px;
    font-family: 'Inter'; font-weight: 700; font-size: 34px;
    color: rgba(255,255,255,0.85);
    letter-spacing: 2px;
    z-index: 10;
  }}
  .badge-num {{
    display: inline-block;
    background: transparent;
    border: 5px solid #F6E600;
    color: #F6E600;
    font-family: 'Bebas Neue'; font-size: 82px;
    padding: 8px 32px 14px;
    border-radius: 16px;
    line-height: 1;
  }}
  .dots-grid {{
    position: absolute;
    background-image: radial-gradient(#F6E600 3px, transparent 3px);
    background-size: 16px 16px;
    opacity: 0.5;
  }}
  .kanji {{
    font-family: 'Noto Serif SC', serif;
    font-weight: 900;
    line-height: 1;
  }}
</style>
</head><body>"""


# =============================================================================
# SLIDE 04 - TARTARUGA (atrás) — versão corrigida
# =============================================================================
s4 = HEAD + """
<div class="canvas" style="background:
  radial-gradient(ellipse at 50% 90%, #0f0f0f 0%, #000 60%);">

  <div class="dots-grid" style="right:40px; top:40px; width:120px; height:120px;"></div>

  <div class="kanji" style="position:absolute; right:-40px; top:100px;
              font-size: 380px; color: #F6E600; opacity: 0.28;
              line-height: 0.85;">龜</div>

  <div style="position:absolute; top:36px; left:50px; display:flex; align-items:center; gap:20px;">
    <div class="badge-num">03</div>
    <div class="pill">TARTARUGA <span style="color:#F6E600">•</span> ATRÁS</div>
  </div>

  <div style="position:absolute; top:210px; left:50px; right:50px;">
    <span class="dash"></span>
    <h1 class="h1" style="font-size: 116px;">
      SEM APOIO<br>
      <span class="hl">ATRÁS</span><span style="color:#FFF;">,</span><br>
      O SONO NÃO<br>
      <span class="hl">CURA</span><span style="color:#FFF;">.</span>
    </h1>
  </div>

  <!-- Diagramas BOM vs RUIM -->
  <div style="position:absolute; top:820px; left:50px; right:50px; height:400px;
              display:grid; grid-template-columns:1fr 1fr; gap:24px;">
    <div style="border:4px solid #22c55e; border-radius:16px; padding:24px 20px;
                background: rgba(34,197,94,0.06);">
      <div style="font-family:'Inter'; font-weight:900; font-size:40px; color:#22c55e;
                  letter-spacing:3px; margin-bottom:12px; text-align:center;">BOM</div>
      <svg viewBox="0 0 220 130" width="100%" height="180">
        <rect x="10" y="10" width="200" height="34" fill="#F6E600" opacity="0.85"/>
        <text x="110" y="34" text-anchor="middle" font-family="Inter" font-size="15" font-weight="900" fill="#111">PAREDE SÓLIDA</text>
        <rect x="40" y="70" width="140" height="50" fill="#666" stroke="#F6E600" stroke-width="2.5" rx="6"/>
        <rect x="55" y="55" width="30" height="22" fill="#999" stroke="#F6E600" stroke-width="1.5" rx="4"/>
        <rect x="135" y="55" width="30" height="22" fill="#999" stroke="#F6E600" stroke-width="1.5" rx="4"/>
      </svg>
      <div style="font-family:'Inter'; font-weight:700; font-size:32px; color:#FFF;
                  text-align:center; margin-top:6px; line-height:1.15;">
        Você dorme<br>protegido
      </div>
    </div>
    <div style="border:4px solid #DC2626; border-radius:16px; padding:24px 20px;
                background: rgba(220,38,38,0.06);">
      <div style="font-family:'Inter'; font-weight:900; font-size:40px; color:#DC2626;
                  letter-spacing:3px; margin-bottom:12px; text-align:center;">RUIM</div>
      <svg viewBox="0 0 220 130" width="100%" height="180">
        <rect x="10" y="10" width="200" height="34" fill="none" stroke="#DC2626" stroke-width="2.5" stroke-dasharray="8,5"/>
        <text x="110" y="34" text-anchor="middle" font-family="Inter" font-size="15" font-weight="900" fill="#DC2626">JANELA / CANO</text>
        <rect x="40" y="70" width="140" height="50" fill="#666" stroke="#DC2626" stroke-width="2.5" rx="6"/>
        <rect x="55" y="55" width="30" height="22" fill="#999" stroke="#DC2626" stroke-width="1.5" rx="4"/>
        <rect x="135" y="55" width="30" height="22" fill="#999" stroke="#DC2626" stroke-width="1.5" rx="4"/>
      </svg>
      <div style="font-family:'Inter'; font-weight:700; font-size:32px; color:#FFF;
                  text-align:center; margin-top:6px; line-height:1.15;">
        Sono raso,<br>cansaço vem
      </div>
    </div>
  </div>

  <div class="footer">03/07</div>
  <div class="swipe"><div class="arrow">&#8594;</div></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(1800)
        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(s4, str(OUT / "slide_04_tartaruga.png"))
