#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Fix slide 5 final: headline e sub sobrepondo. Move sub pra bem embaixo."""
import os
from pathlib import Path
from playwright.sync_api import sync_playwright
import tempfile

OUT = Path("/opt/mia/workspace/clientes/borrello/criativos_ig_teste_v3_2026-09-18/carrossel_v3_com_acentos")
W, H = 1080, 1350


def head():
    return 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;
  }}
  .h1 {{
    font-family: 'Bebas Neue', sans-serif; font-weight: 400;
    text-transform: uppercase; line-height: 1.02;
    letter-spacing: 1px; color: #FFF;
  }}
  .hl {{
    background: #F6E600; color: #111;
    padding: 6px 22px 14px 22px; border-radius: 10px;
    display: inline-block; line-height: 0.92; margin: 4px 0;
  }}
  .sub {{
    font-family: 'Inter'; font-weight: 600;
    font-size: 50px; line-height: 1.22; color: #F5F5F5;
  }}
  .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;
  }}
  .kanji {{
    font-family: 'Noto Serif SC', serif;
    font-weight: 900;
    line-height: 1;
  }}
</style>
</head><body>"""


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(1600)
        page.screenshot(path=dest, clip={"x": 0, "y": 0, "width": W, "height": H})
        browser.close()
    os.unlink(tmp.name)
    print(f"  fix OK -> {dest}")


# Slide 5 refeito: kanji menor no topo, headline centro, sub em painel escuro no rodapé
s5 = head() + """
<div class="canvas" style="background:
  radial-gradient(ellipse at 50% 22%, #4a1010 0%, #200606 40%, #000 75%);">

  <!-- Kanji fênix no topo, 380pt (não domina toda a tela) -->
  <div class="kanji" style="position:absolute; left:50%; top:80px;
              transform:translateX(-50%);
              font-size: 380px; color: #F6E600; opacity: 0.95;
              text-shadow: 0 0 100px rgba(246,230,0,0.6);
              line-height: 0.85;">鳳</div>

  <svg width="1080" height="440" viewBox="0 0 1080 440"
       style="position:absolute; left:0; top:80px; opacity:0.28;">
    <g stroke="#F6E600" stroke-width="3" fill="none">
      <line x1="540" y1="220" x2="120" y2="40"/>
      <line x1="540" y1="220" x2="960" y2="40"/>
      <line x1="540" y1="220" x2="60" y2="220"/>
      <line x1="540" y1="220" x2="1020" y2="220"/>
      <line x1="540" y1="220" x2="120" y2="400"/>
      <line x1="540" y1="220" x2="960" y2="400"/>
    </g>
  </svg>

  <div style="position:absolute; top:36px; right:50px; display:flex; align-items:center; gap:20px;">
    <div class="pill">FÊNIX <span style="color:#F6E600">•</span> FRENTE</div>
    <div class="badge-num">04</div>
  </div>

  <!-- Headline no meio -->
  <div style="position:absolute; top:600px; left:50px; right:50px; text-align:center;">
    <div style="width:120px; height:8px; background:#F6E600; margin:0 auto 20px;"></div>
    <h1 class="h1" style="font-size: 88px;">
      NA <span class="hl">FRENTE</span>,<br>
      A FÊNIX PRECISA<br>
      DE <span class="hl">ESPAÇO</span><br>
      PRA VOAR.
    </h1>
  </div>

  <!-- Sub bem em baixo, com fundo escuro -->
  <div style="position:absolute; bottom:140px; left:40px; right:40px; text-align:center;
              background: rgba(0,0,0,0.75); padding: 18px 22px; border-radius:14px;
              border: 2px solid rgba(246,230,0,0.2);">
    <p class="sub" style="font-size: 40px; line-height:1.22;">
      Prédio colado, muro alto,<br>
      garagem fechada = <b>fênix presa</b>.<br>
      <b>Oportunidade não chega</b>.
    </p>
  </div>

  <div class="footer">04/07</div>
  <div class="swipe"><div class="arrow">&#8594;</div></div>
</div>
</body></html>
"""
render(s5, str(OUT / "slide_05_fenix.png"))
print("OK slide 5 final")
