#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Correcao dos slides 5 (fenix) e 6 (serpente) do carrossel v3.
Problemas identificados no primeiro render:
  - Slide 5: kanji fenix sobrepunha o texto "PRA VOAR" da headline
  - Slide 6: circulo amarelo cobria parte do sub 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; }}
  .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: '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; }}
  .dots-grid {{ position: absolute; background-image: radial-gradient(#F6E600 3px, transparent 3px);
    background-size: 16px 16px; opacity: 0.5; }}
</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 corrigido
# Kanji fica no TOPO (nao mais atras da headline), headline abaixo do kanji,
# sub embaixo. Layout vertical em 3 camadas com boa separacao.
# =============================================================================
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; z-index:5;">
    <div class="pill">FENIX <span style="color:#F6E600">•</span> FRENTE</div>
    <div class="badge-num">04</div>
  </div>

  <!-- Kanji fenix - no meio, altura controlada 380px de altura visual -->
  <div class="kanji" style="position:absolute; left:50%; top:230px;
              transform:translateX(-50%);
              font-size: 400px; color: #F6E600; opacity: 0.95;
              text-shadow: 0 0 80px rgba(246,230,0,0.55);
              line-height: 0.85; z-index:2;">鳳</div>

  <!-- Raios de fundo -->
  <svg width="1080" height="500" viewBox="0 0 1080 500"
       style="position:absolute; left:0; top:180px; opacity:0.25; z-index:1;">
    <g stroke="#F6E600" stroke-width="3" fill="none">
      <line x1="540" y1="250" x2="120" y2="60"/>
      <line x1="540" y1="250" x2="960" y2="60"/>
      <line x1="540" y1="250" x2="40" y2="250"/>
      <line x1="540" y1="250" x2="1040" y2="250"/>
      <line x1="540" y1="250" x2="120" y2="450"/>
      <line x1="540" y1="250" x2="960" y2="450"/>
    </g>
  </svg>

  <!-- Headline (abaixo do kanji, sem overlap) -->
  <div style="position:absolute; top:680px; left:50px; right:50px; text-align:center; z-index:3;">
    <div style="width:120px; height:8px; background:#F6E600; margin:0 auto 20px;"></div>
    <h1 class="h1" style="font-size: 100px;">
      NA <span class="hl">FRENTE</span>,<br>
      PRECISA DE<br>
      <span class="hl">ESPACO</span><br>
      PRA VOAR.
    </h1>
  </div>

  <!-- Sub embaixo -->
  <div style="position:absolute; bottom:170px; left:50px; right:50px; text-align:center; z-index:3;">
    <p class="sub" style="font-size: 44px; line-height:1.2;">
      Predio colado, muro alto:<br>
      <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"))


# =============================================================================
# SLIDE 06 - SERPENTE corrigido
# Circulo amarelo com kanji fica menor (480px) e mais alto para nao cobrir o sub
# Sub agora fica ACIMA do circulo (nao embaixo)
# =============================================================================
s6 = head() + """
<div class="canvas" style="background:
  radial-gradient(ellipse at 50% 60%, #2a2010 0%, #000 70%);">

  <div class="dots-grid" style="right:40px; top:40px; width:120px; height:120px;"></div>
  <div class="dots-grid" style="left:40px; bottom:160px; width:100px; height:100px; opacity:0.35;"></div>

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

  <!-- Headline topo -->
  <div style="position:absolute; top:220px; left:50px; right:50px;">
    <span class="dash"></span>
    <h1 class="h1" style="font-size: 108px;">
      NO CENTRO<br>
      DA CASA<br>
      MORA <span class="hl">VOCE</span>.
    </h1>
  </div>

  <!-- Circulo amarelo (menor, 480x480, posicionado meio-baixo mas com espaco pro texto embaixo) -->
  <div style="position:absolute; left:50%; top:920px; transform:translate(-50%,-50%);
              width:480px; height:480px; border-radius:50%;
              background: radial-gradient(circle, #F6E600 0%, #B8860B 78%, #6B5008 100%);
              box-shadow: 0 0 100px rgba(246,230,0,0.5);
              display:flex; align-items:center; justify-content:center;">
    <div class="kanji" style="font-size: 340px; color: #111; line-height: 0.85;">蛇</div>
  </div>

  <!-- Sub AGORA à ESQUERDA, ao lado do circulo -->
  <div style="position:absolute; top:790px; left:50px; width:280px;">
    <p style="font-family:'Inter'; font-weight:700; font-size:38px; line-height:1.15; color:#FFF;">
      Miolo<br>
      escuro?<br>
      <span style="color:#F6E600;">Vira o<br>centro<br>da sua<br>vida.</span>
    </p>
  </div>

  <div class="footer">05/07</div>
  <div class="swipe"><div class="arrow">&#8594;</div></div>
</div>
</body></html>
"""
render(s6, str(OUT / "slide_06_serpente.png"))

print("\nOK, slides 5 e 6 corrigidos.")
