#!/usr/bin/env python3
"""Artes v4 - Francisco Borrello (ajustes de feedback do cliente)

Mudancas vs v3:
- Removido bloco "A JORNADA OFICIAL DE" (bridge) das 3 artes
- Foto do professor espelhada horizontalmente (foto_oficial_cutout_mirror.png)
- Banner desktop (1290x500): professor centralizado com textos na esquerda,
  cabeca visivel completa, pode cortar abaixo da cintura

Saidas (sobrescreve v3):
  - hotmart_capa_v3.jpg    1080x1080  (capa Hotmart)
  - checkout_1x1.jpg       1080x1080  (banner checkout mobile)
  - checkout_desktop.jpg   1290x500   (banner checkout desktop)
"""
from playwright.sync_api import sync_playwright
from pathlib import Path
import base64

BASE = Path("/opt/mia/workspace/clientes/francisco_borrello")
BG = BASE / "_bg_cromoterapia.png"
FOTO = BASE / "_recortes" / "foto_oficial_cutout_mirror.png"  # espelhada

FONTS = Path("/home/mia/.fonts/px3lab")
BEBAS = FONTS / "BebasNeue-Regular.ttf"
INTER_BOLD = FONTS / "Inter-Bold.ttf"
INTER_REG = FONTS / "Inter-Regular.ttf"
INTER_SEMI = FONTS / "Inter-SemiBold.ttf"


def b64(p: Path) -> str:
    return base64.b64encode(p.read_bytes()).decode()


bg_b64 = b64(BG)
foto_b64 = b64(FOTO)
bebas_b64 = b64(BEBAS)
inter_bold_b64 = b64(INTER_BOLD)
inter_reg_b64 = b64(INTER_REG)
inter_semi_b64 = b64(INTER_SEMI)


COMMON_FONTS = f"""
@font-face {{ font-family: 'Bebas Neue'; src: url(data:font/ttf;base64,{bebas_b64}) format('truetype'); font-weight: 400; }}
@font-face {{ font-family: 'Inter'; src: url(data:font/ttf;base64,{inter_bold_b64}) format('truetype'); font-weight: 700; }}
@font-face {{ font-family: 'Inter'; src: url(data:font/ttf;base64,{inter_semi_b64}) format('truetype'); font-weight: 600; }}
@font-face {{ font-family: 'Inter'; src: url(data:font/ttf;base64,{inter_reg_b64}) format('truetype'); font-weight: 400; }}
"""

# =============================================================================
# ARTE 1 e 2 - 1080x1080 (capa Hotmart e checkout mobile)
# Coluna esquerda com todos os textos; foto do professor a direita (espelhada)
# SEM o bloco "A jornada oficial de"
# =============================================================================

def html_square(tag_texto: str, show_selos: bool = False) -> str:
    selos_html = ""
    if show_selos:
        selos_html = """
  <div class="selos">
    <div class="selo">
      <svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2">
        <path d="M12 2L4 6v6c0 5 3.5 9.5 8 10 4.5-.5 8-5 8-10V6l-8-4z"/>
      </svg>
      <span>COMPRA<br>100% SEGURA</span>
    </div>
    <div class="selo-sep"></div>
    <div class="selo">
      <svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2">
        <circle cx="12" cy="12" r="9"/>
        <path d="M8 12l3 3 5-6"/>
      </svg>
      <span>GARANTIA<br>7 DIAS</span>
    </div>
    <div class="selo-sep"></div>
    <div class="selo">
      <svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2">
        <rect x="3" y="6" width="18" height="12" rx="2"/>
        <path d="M3 10h18"/>
      </svg>
      <span>PAGAMENTO<br>PROTEGIDO</span>
    </div>
  </div>
"""

    return f"""<!doctype html>
<html lang="pt-BR"><head><meta charset="utf-8">
<style>
{COMMON_FONTS}

* {{ box-sizing: border-box; margin: 0; padding: 0; }}
html, body {{ width: 1080px; height: 1080px; overflow: hidden; }}
body {{
  position: relative;
  font-family: 'Inter', sans-serif;
  color: #fff;
  background:
    linear-gradient(90deg, rgba(6,4,14,0.72) 0%, rgba(6,4,14,0.55) 50%, rgba(6,4,14,0.22) 80%, rgba(6,4,14,0.05) 100%),
    url(data:image/png;base64,{bg_b64}) center/cover no-repeat;
}}

.glow-violet {{
  position: absolute;
  top: 180px; left: 20px;
  width: 620px; height: 280px;
  background: radial-gradient(ellipse at center, rgba(168,148,245,0.5) 0%, rgba(168,148,245,0.18) 42%, transparent 72%);
  filter: blur(30px);
  pointer-events: none;
}}
.glow-gold {{
  position: absolute;
  top: 440px; left: 20px;
  width: 620px; height: 320px;
  background: radial-gradient(ellipse at center, rgba(226,174,90,0.42) 0%, rgba(226,174,90,0.16) 40%, transparent 70%);
  filter: blur(30px);
  pointer-events: none;
}}

.halo-figure {{
  position: absolute;
  right: -140px; bottom: -80px;
  width: 800px; height: 800px;
  background: radial-gradient(circle at center, rgba(226,174,90,0.28) 0%, rgba(168,148,245,0.16) 40%, transparent 68%);
  filter: blur(40px);
  pointer-events: none;
}}

.corner {{
  position: absolute; width: 44px; height: 44px;
  border-color: rgba(226,174,90,0.55);
  border-style: solid; border-width: 0;
}}
.corner.tl {{ top: 32px; left: 32px; border-top-width: 2px; border-left-width: 2px; }}
.corner.tr {{ top: 32px; right: 32px; border-top-width: 2px; border-right-width: 2px; }}
.corner.bl {{ bottom: 32px; left: 32px; border-bottom-width: 2px; border-left-width: 2px; }}
.corner.br {{ bottom: 32px; right: 32px; border-bottom-width: 2px; border-right-width: 2px; }}

/* Bloco textual esquerda - largura maxima 560px para nao bater na foto */
.left {{
  position: absolute;
  top: 175px; left: 70px;
  display: flex; flex-direction: column;
  align-items: flex-start;
  width: 560px;
  z-index: 3;
}}

.tag {{
  border: 1px solid rgba(226,174,90,0.65);
  background: rgba(6,4,14,0.35);
  border-radius: 999px;
  padding: 9px 24px;
  font-family: 'Inter', sans-serif;
  font-weight: 600;
  font-size: 12px;
  letter-spacing: 4.5px;
  color: #f4d9a4;
  text-transform: uppercase;
}}

/* Titulo 1 - RADIESTESIA (violeta claro) */
.radi {{
  font-family: 'Bebas Neue', sans-serif;
  font-size: 132px;
  line-height: 0.9;
  letter-spacing: 3px;
  margin-top: 28px;
  background: linear-gradient(180deg, #ffffff 0%, #d8c9ff 55%, #a894f5 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 0 45px rgba(168,148,245,0.4);
  filter: drop-shadow(0 6px 14px rgba(0,0,0,0.65));
}}

/* Titulo 2 - com Cromoterapia (dourado) - vem direto sem bridge */
.cromo {{
  font-family: 'Bebas Neue', sans-serif;
  font-size: 82px;
  line-height: 0.94;
  letter-spacing: 2px;
  margin-top: 22px;
  background: linear-gradient(180deg, #f6dfa8 0%, #e2ae5a 45%, #b6832f 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 0 40px rgba(226,174,90,0.35);
  filter: drop-shadow(0 6px 14px rgba(0,0,0,0.65));
}}

/* Titulo 3 - das Luzes Angelicais (dourado, 2 linhas) */
.luzes {{
  font-family: 'Bebas Neue', sans-serif;
  font-size: 70px;
  line-height: 0.95;
  letter-spacing: 2px;
  margin-top: 8px;
  background: linear-gradient(180deg, #f6dfa8 0%, #e2ae5a 45%, #b6832f 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 0 40px rgba(226,174,90,0.35);
  filter: drop-shadow(0 6px 14px rgba(0,0,0,0.65));
}}

/* Metodo exclusivo (branco) */
.metodo {{
  font-family: 'Inter', sans-serif;
  font-weight: 600;
  font-size: 14px;
  letter-spacing: 6px;
  color: rgba(255,255,255,0.9);
  margin-top: 38px;
  text-transform: uppercase;
  text-shadow: 0 2px 8px rgba(0,0,0,0.7);
}}

/* Nome do professor (dourado claro) */
.prof-nome {{
  font-family: 'Bebas Neue', sans-serif;
  font-size: 34px;
  letter-spacing: 3px;
  color: #f4d9a4;
  margin-top: 6px;
  text-shadow: 0 2px 12px rgba(0,0,0,0.7);
}}

/* Foto do professor - lado direito, empurrada mais pra direita */
.figure-wrap {{
  position: absolute;
  right: -40px; bottom: 0;
  height: 1000px;
  z-index: 5;
}}
.figure-wrap img {{
  height: 1000px;
  width: auto;
  display: block;
  filter: drop-shadow(0 12px 32px rgba(0,0,0,0.6)) drop-shadow(0 0 40px rgba(168,148,245,0.20));
}}
.figure-fade {{
  position: absolute;
  right: -40px; bottom: 0;
  width: 620px; height: 140px;
  background: linear-gradient(180deg, transparent 0%, rgba(6,4,14,0.85) 100%);
  z-index: 6;
  pointer-events: none;
}}

/* Selos (opcional - so no checkout_1x1) */
.selos {{
  position: absolute;
  left: 70px; bottom: 60px;
  z-index: 7;
  display: flex; align-items: center;
  gap: 18px;
  background: rgba(6,4,14,0.55);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(226,174,90,0.32);
  border-radius: 14px;
  padding: 14px 22px;
}}
.selo {{
  display: flex; align-items: center; gap: 10px;
  color: #f4d9a4;
}}
.selo span {{
  font-family: 'Inter', sans-serif;
  font-weight: 700;
  font-size: 10px;
  letter-spacing: 1.4px;
  line-height: 1.15;
  color: #ffffff;
  text-transform: uppercase;
}}
.selo-sep {{
  width: 1px; height: 26px;
  background: rgba(226,174,90,0.28);
}}
</style></head>
<body>
  <div class="glow-violet"></div>
  <div class="glow-gold"></div>
  <div class="halo-figure"></div>

  <div class="corner tl"></div>
  <div class="corner tr"></div>
  <div class="corner bl"></div>
  <div class="corner br"></div>

  <div class="figure-wrap">
    <img src="data:image/png;base64,{foto_b64}" alt="Prof. Francisco Borrello" />
  </div>
  <div class="figure-fade"></div>

  <div class="left">
    <div class="tag">{tag_texto}</div>
    <div class="radi">RADIESTESIA</div>
    <div class="cromo">com Cromoterapia</div>
    <div class="luzes">das Luzes<br>Angelicais</div>
    <div class="metodo">METODO EXCLUSIVO</div>
    <div class="prof-nome">PROF. FRANCISCO BORRELLO</div>
  </div>

  {selos_html}
</body></html>
"""


# =============================================================================
# ARTE 3 - 1290x500 (banner checkout desktop)
# Professor CENTRALIZADO horizontalmente, textos a esquerda
# Cabeca visivel, pode cortar cintura pra baixo
# SEM o bloco "A jornada oficial de"
# =============================================================================

HTML_DESKTOP = f"""<!doctype html>
<html lang="pt-BR"><head><meta charset="utf-8">
<style>
{COMMON_FONTS}

* {{ box-sizing: border-box; margin: 0; padding: 0; }}
html, body {{ width: 1290px; height: 500px; overflow: hidden; }}
body {{
  position: relative;
  font-family: 'Inter', sans-serif;
  color: #fff;
  background:
    linear-gradient(90deg, rgba(6,4,14,0.85) 0%, rgba(6,4,14,0.72) 40%, rgba(6,4,14,0.45) 60%, rgba(6,4,14,0.30) 80%, rgba(6,4,14,0.15) 100%),
    url(data:image/png;base64,{bg_b64}) center/cover no-repeat;
}}

.glow-violet {{
  position: absolute;
  top: 30px; left: 40px;
  width: 560px; height: 190px;
  background: radial-gradient(ellipse at center, rgba(168,148,245,0.42) 0%, rgba(168,148,245,0.16) 42%, transparent 72%);
  filter: blur(30px);
  pointer-events: none;
}}
.glow-gold {{
  position: absolute;
  top: 200px; left: 40px;
  width: 560px; height: 240px;
  background: radial-gradient(ellipse at center, rgba(226,174,90,0.36) 0%, rgba(226,174,90,0.13) 40%, transparent 70%);
  filter: blur(30px);
  pointer-events: none;
}}
.halo-figure {{
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  width: 620px; height: 500px;
  background: radial-gradient(ellipse at center, rgba(226,174,90,0.28) 0%, rgba(168,148,245,0.15) 40%, transparent 68%);
  filter: blur(40px);
  pointer-events: none;
}}

.corner {{
  position: absolute; width: 38px; height: 38px;
  border-color: rgba(226,174,90,0.55);
  border-style: solid; border-width: 0;
}}
.corner.tl {{ top: 24px; left: 24px; border-top-width: 2px; border-left-width: 2px; }}
.corner.tr {{ top: 24px; right: 24px; border-top-width: 2px; border-right-width: 2px; }}
.corner.bl {{ bottom: 24px; left: 24px; border-bottom-width: 2px; border-left-width: 2px; }}
.corner.br {{ bottom: 24px; right: 24px; border-bottom-width: 2px; border-right-width: 2px; }}

/* Textos na metade esquerda (max 560px) */
.left {{
  position: absolute;
  top: 74px; left: 60px;
  display: flex; flex-direction: column;
  align-items: flex-start;
  max-width: 560px;
  z-index: 3;
}}

.tag {{
  border: 1px solid rgba(226,174,90,0.65);
  background: rgba(6,4,14,0.35);
  border-radius: 999px;
  padding: 6px 18px;
  font-family: 'Inter', sans-serif;
  font-weight: 600;
  font-size: 10px;
  letter-spacing: 4px;
  color: #f4d9a4;
  text-transform: uppercase;
}}

.radi {{
  font-family: 'Bebas Neue', sans-serif;
  font-size: 82px;
  line-height: 0.9;
  letter-spacing: 3px;
  margin-top: 14px;
  background: linear-gradient(180deg, #ffffff 0%, #d8c9ff 55%, #a894f5 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 0 40px rgba(168,148,245,0.4);
  filter: drop-shadow(0 4px 10px rgba(0,0,0,0.65));
}}

.cromo {{
  font-family: 'Bebas Neue', sans-serif;
  font-size: 52px;
  line-height: 0.95;
  letter-spacing: 2px;
  margin-top: 14px;
  background: linear-gradient(180deg, #f6dfa8 0%, #e2ae5a 45%, #b6832f 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 0 30px rgba(226,174,90,0.35);
  filter: drop-shadow(0 3px 8px rgba(0,0,0,0.65));
}}
.luzes {{
  font-family: 'Bebas Neue', sans-serif;
  font-size: 46px;
  line-height: 0.95;
  letter-spacing: 2px;
  margin-top: 4px;
  background: linear-gradient(180deg, #f6dfa8 0%, #e2ae5a 45%, #b6832f 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 0 30px rgba(226,174,90,0.35);
  filter: drop-shadow(0 3px 8px rgba(0,0,0,0.65));
}}

/* Rodape com metodo e nome do prof - alinhado embaixo esquerda */
.footer {{
  position: absolute;
  left: 60px; bottom: 40px;
  z-index: 4;
  display: flex; align-items: center; gap: 14px;
}}
.footer .metodo {{
  font-family: 'Inter', sans-serif;
  font-weight: 600;
  font-size: 10px;
  letter-spacing: 4px;
  color: rgba(255,255,255,0.9);
  text-transform: uppercase;
  text-shadow: 0 2px 8px rgba(0,0,0,0.7);
}}
.footer .sep {{
  width: 22px; height: 1px;
  background: rgba(226,174,90,0.7);
}}
.footer .prof-nome {{
  font-family: 'Bebas Neue', sans-serif;
  font-size: 22px;
  letter-spacing: 2.5px;
  color: #f4d9a4;
  text-shadow: 0 2px 8px rgba(0,0,0,0.7);
}}

/* Foto centralizada horizontalmente no banner.
   Recorte da foto tem head no topo. Com height 640 e viewport 500,
   corta ~140px abaixo (cintura). Cabeca fica visivel. */
.figure-wrap {{
  position: absolute;
  left: 50%; bottom: -100px;
  transform: translateX(-50%);
  height: 640px;
  z-index: 2;
}}
.figure-wrap img {{
  height: 640px;
  width: auto;
  display: block;
  filter: drop-shadow(0 8px 24px rgba(0,0,0,0.55)) drop-shadow(0 0 30px rgba(168,148,245,0.18));
}}
.figure-fade {{
  position: absolute;
  left: 50%; bottom: 0;
  transform: translateX(-50%);
  width: 380px; height: 60px;
  background: linear-gradient(180deg, transparent 0%, rgba(6,4,14,0.9) 100%);
  z-index: 3;
  pointer-events: none;
}}
</style></head>
<body>
  <div class="glow-violet"></div>
  <div class="glow-gold"></div>
  <div class="halo-figure"></div>

  <div class="corner tl"></div>
  <div class="corner tr"></div>
  <div class="corner bl"></div>
  <div class="corner br"></div>

  <div class="figure-wrap">
    <img src="data:image/png;base64,{foto_b64}" alt="Prof. Francisco Borrello" />
  </div>
  <div class="figure-fade"></div>

  <div class="left">
    <div class="tag">Acesso Oficial Hotmart</div>
    <div class="radi">RADIESTESIA</div>
    <div class="cromo">com Cromoterapia</div>
    <div class="luzes">das Luzes Angelicais</div>
  </div>

  <div class="footer">
    <span class="metodo">METODO EXCLUSIVO</span>
    <span class="sep"></span>
    <span class="prof-nome">PROF. FRANCISCO BORRELLO</span>
  </div>
</body></html>
"""


def render_html(html: str, out_png: Path, out_jpg: Path, w: int, h: int):
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page(viewport={"width": w, "height": h}, device_scale_factor=2)
        page.set_content(html, wait_until="networkidle")
        page.screenshot(path=str(out_png), clip={"x": 0, "y": 0, "width": w, "height": h})
        browser.close()

    from PIL import Image
    img = Image.open(out_png).convert("RGB")
    if img.size != (w, h):
        img = img.resize((w, h), Image.LANCZOS)
    img.save(out_jpg, "JPEG", quality=92, optimize=True)
    print(f"OK -> {out_jpg}  ({img.size[0]}x{img.size[1]})")


if __name__ == "__main__":
    # ARTE 1 - capa Hotmart (sem selos)
    render_html(
        html_square(tag_texto="Curso Oficial Hotmart", show_selos=False),
        BASE / "hotmart_capa_v3.png",
        BASE / "hotmart_capa_v3.jpg",
        1080, 1080,
    )
    # ARTE 2 - checkout mobile (com selos)
    render_html(
        html_square(tag_texto="Acesso Oficial Hotmart", show_selos=True),
        BASE / "checkout_1x1.png",
        BASE / "checkout_1x1.jpg",
        1080, 1080,
    )
    # ARTE 3 - checkout desktop (professor centralizado)
    render_html(
        HTML_DESKTOP,
        BASE / "checkout_desktop.png",
        BASE / "checkout_desktop.jpg",
        1290, 500,
    )
