#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Correcao final slide 5 (fenix): kanji no TOPO menor, headline media, sub bem separado embaixo.
"""
import os, 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_v2_fonte_grande")
W, H = 1080, 1350


def head():
    return f"""<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<link href="https://fonts.googleapis.com/css2?family=Anton&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'; font-weight: 800; font-size: 32px;
    letter-spacing: 2px; color: #FFF; }}
  .h1 {{ font-family: 'Anton', sans-serif; text-transform: uppercase; line-height: 1.0; letter-spacing: -0.5px; color: #FFF; }}
  .hl {{ background: #F6E600; color: #111; padding: 6px 22px 14px 22px; border-radius: 10px;
    display: inline-block; line-height: 0.9; 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: 'Anton'; 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(1400)
        page.screenshot(path=dest, clip={"x": 0, "y": 0, "width": W, "height": H})
        browser.close()
    os.unlink(tmp.name)
    print(f"  render OK -> {dest}")


# SLIDE 05 - FENIX (v2 fix)
# Layout: topo=pill+badge, kanji medio 300pt logo abaixo, headline 88pt bem separada, sub embaixo
s5 = head() + """
<div class="canvas" style="background:
  radial-gradient(ellipse at 50% 25%, #4a1010 0%, #200606 45%, #000 80%);">

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

  <!-- Kanji fenix - medio (300pt) no topo, delimitado -->
  <div class="kanji" style="position:absolute; left:50%; top:200px;
              transform:translateX(-50%);
              font-size: 340px; color: #F6E600; opacity: 0.95;
              text-shadow: 0 0 80px rgba(246,230,0,0.55);
              line-height: 0.85;">鳳</div>

  <!-- Raios sutis atras -->
  <svg width="1080" height="400" viewBox="0 0 1080 400"
       style="position:absolute; left:0; top:180px; opacity:0.22;">
    <g stroke="#F6E600" stroke-width="3" fill="none">
      <line x1="540" y1="200" x2="120" y2="50"/>
      <line x1="540" y1="200" x2="960" y2="50"/>
      <line x1="540" y1="200" x2="40" y2="200"/>
      <line x1="540" y1="200" x2="1040" y2="200"/>
      <line x1="540" y1="200" x2="120" y2="360"/>
      <line x1="540" y1="200" x2="960" y2="360"/>
    </g>
  </svg>

  <!-- Headline - abaixo do kanji, em 88pt, centralizada, 4 linhas -->
  <div style="position:absolute; top:620px; left:50px; right:50px; text-align:center;">
    <div style="width:120px; height:8px; background:#F6E600; margin:0 auto 24px;"></div>
    <h1 class="h1" style="font-size: 92px;">
      NA <span class="hl">FRENTE</span>,<br>
      PRECISA DE<br>
      <span class="hl">ESPACO</span>.
    </h1>
  </div>

  <!-- Sub embaixo bem separado, alinhado centro -->
  <div style="position:absolute; bottom:180px; left:50px; right:50px; text-align:center;">
    <p class="sub" style="font-size: 44px; line-height:1.22; color:#FFF;">
      Predio colado ou<br>
      muro alto = <b>fenix presa</b>.<br>
      Oportunidade <b>nao 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 v2 corrigido")
