import base64
from pathlib import Path
from playwright.sync_api import sync_playwright

FRAN = Path("/opt/mia/workspace/clientes/borrello/fotos_banner/salespage_assets/fran.png")
OUT_PNG = Path("/opt/mia/workspace/clientes/borrello/banner_mesas_radionicas_v2.png")
OUT_JPG = Path("/opt/mia/workspace/clientes/borrello/banner_mesas_radionicas_v2.jpg")

fran_b64 = base64.b64encode(FRAN.read_bytes()).decode("ascii")
fran_data = f"data:image/png;base64,{fran_b64}"

# LAYOUT PLANEJADO (banner 1920x600):
# - padding lateral: 100px cada lado
# - foto: 420px de largura, encostada na direita (com 100px de padding)
# - texto: bloco centralizado ocupando o restante (100px esq -> inicio foto - gap)
# content: left=100, width=1920 - 100 (esq) - 100 (dir pad) - 420 (foto) - 40 (gap) = 1260

HTML = 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=Playfair+Display:ital,wght@0,400;0,600;0,700;0,800;1,400;1,600&family=Cormorant+Garamond:wght@400;500;600;700&family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
<style>
  * {{ box-sizing: border-box; margin: 0; padding: 0; }}
  html, body {{ margin: 0; padding: 0; overflow: hidden; background: #0a1226; }}
  body {{
    width: 1920px;
    height: 600px;
    font-family: 'Inter', sans-serif;
    color: #ecebe4;
  }}

  .banner {{
    position: relative;
    width: 1920px;
    height: 600px;
    background:
      radial-gradient(900px 500px at 78% 55%, rgba(196,161,90,0.16) 0%, rgba(196,161,90,0) 60%),
      radial-gradient(900px 500px at 22% 50%, rgba(20,32,60,0.9) 0%, rgba(10,18,38,0.9) 60%),
      linear-gradient(135deg, #060c1c 0%, #0b1530 50%, #0a1226 100%);
    overflow: hidden;
  }}

  /* textura fina de linhas douradas no fundo */
  .banner::before {{
    content: "";
    position: absolute; inset: 0;
    background-image:
      linear-gradient(90deg, rgba(196,161,90,0.045) 1px, transparent 1px),
      linear-gradient(0deg, rgba(196,161,90,0.028) 1px, transparent 1px);
    background-size: 80px 80px, 80px 80px;
    pointer-events: none;
    mask-image: radial-gradient(ellipse at center, rgba(0,0,0,0.7) 0%, rgba(0,0,0,0) 75%);
  }}

  /* moldura interna dourada sutil */
  .banner::after {{
    content: "";
    position: absolute;
    top: 24px; left: 24px; right: 24px; bottom: 24px;
    border: 1px solid rgba(196,161,90,0.22);
    pointer-events: none;
    z-index: 4;
  }}

  /* CONTEUDO CENTRAL - texto centralizado como paragrafo */
  .content {{
    position: absolute;
    left: 100px;
    top: 50px;
    width: 1260px;
    height: 500px;
    z-index: 2;
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 14px;
  }}

  .eyebrow {{
    font-family: 'Inter', sans-serif;
    font-size: 13px;
    letter-spacing: 0.42em;
    text-transform: uppercase;
    color: #c4a15a;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 14px;
  }}
  .eyebrow .line {{
    width: 44px; height: 1px; background: #c4a15a; opacity: 0.7;
  }}

  .headline {{
    font-family: 'Playfair Display', serif;
    font-weight: 700;
    font-size: 46px;
    line-height: 1.08;
    letter-spacing: 0.01em;
    color: #f5f1e6;
  }}
  .headline .dot {{
    color: #c4a15a;
    margin: 0 12px;
    font-weight: 400;
  }}
  .headline .accent {{
    color: #c4a15a;
    font-style: italic;
    font-weight: 600;
  }}

  .subline {{
    font-family: 'Cormorant Garamond', serif;
    font-style: italic;
    font-weight: 500;
    font-size: 25px;
    color: #d6d1c1;
    line-height: 1.25;
    max-width: 1000px;
  }}

  .offer {{
    background: linear-gradient(135deg, rgba(196,161,90,0.12) 0%, rgba(196,161,90,0.03) 100%);
    border: 1px solid rgba(196,161,90,0.4);
    padding: 14px 32px;
    border-radius: 4px;
    max-width: 1100px;
  }}
  .offer-text {{
    font-family: 'Cormorant Garamond', serif;
    font-size: 22px;
    line-height: 1.35;
    color: #ecebe4;
    font-weight: 500;
  }}
  .offer-text .kit {{
    font-family: 'Playfair Display', serif;
    font-weight: 800;
    font-size: 34px;
    color: #d9b56a;
    letter-spacing: 0.02em;
    text-shadow: 0 0 22px rgba(217,181,106,0.38);
    display: inline-block;
    padding: 0 4px;
  }}

  .bottom-row {{
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 34px;
    flex-wrap: nowrap;
    margin-top: 4px;
  }}

  .price {{
    display: flex;
    align-items: baseline;
    gap: 12px;
    color: #ecebe4;
    font-family: 'Inter', sans-serif;
  }}
  .price .from {{
    font-size: 14px;
    color: #8a8778;
    text-decoration: line-through;
    letter-spacing: 0.05em;
  }}
  .price .by {{
    font-size: 12px;
    color: #a8a394;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    margin-right: 2px;
  }}
  .price .value {{
    font-family: 'Playfair Display', serif;
    font-size: 44px;
    font-weight: 700;
    color: #f5f1e6;
    letter-spacing: 0.01em;
    line-height: 1;
  }}
  .price .value .rs {{
    font-size: 22px;
    color: #c4a15a;
    margin-right: 4px;
    font-weight: 500;
  }}
  .price .install {{
    font-size: 14px;
    color: #b8b3a3;
    letter-spacing: 0.05em;
    border-left: 1px solid rgba(196,161,90,0.4);
    padding-left: 14px;
  }}

  .cta {{
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 17px 36px;
    background: linear-gradient(135deg, #d9b56a 0%, #b8944a 100%);
    color: #0a1226;
    font-family: 'Inter', sans-serif;
    font-weight: 800;
    font-size: 15px;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    border-radius: 3px;
    box-shadow: 0 6px 22px rgba(217,181,106,0.28);
    border: 1px solid #e6c684;
    white-space: nowrap;
  }}

  .footer {{
    font-family: 'Inter', sans-serif;
    font-size: 11px;
    color: #8a8778;
    letter-spacing: 0.28em;
    text-transform: uppercase;
    margin-top: 8px;
  }}
  .footer .sep {{
    color: #c4a15a;
    margin: 0 12px;
  }}

  /* COLUNA DA FOTO - direita, ~1/3 do banner */
  .photo-col {{
    position: absolute;
    right: 100px;
    top: 0;
    bottom: 0;
    width: 420px;
    z-index: 3;
    display: flex;
    align-items: flex-end;
    justify-content: center;
  }}

  .photo-glow {{
    position: absolute;
    bottom: -10px;
    left: 50%;
    transform: translateX(-50%);
    width: 360px;
    height: 360px;
    background: radial-gradient(circle, rgba(196,161,90,0.28) 0%, rgba(196,161,90,0) 65%);
    filter: blur(24px);
    z-index: 1;
  }}

  .photo-frame img {{
    position: relative;
    z-index: 2;
    height: 560px;
    width: auto;
    max-width: 420px;
    object-fit: contain;
    object-position: bottom center;
    filter: drop-shadow(0 20px 40px rgba(0,0,0,0.55));
  }}

  .photo-badge {{
    position: absolute;
    top: 60px;
    right: 100px;
    z-index: 5;
    text-align: right;
    font-family: 'Inter', sans-serif;
    padding: 8px 14px 8px 18px;
    border-right: 2px solid #c4a15a;
    background: linear-gradient(90deg, rgba(10,18,38,0) 0%, rgba(10,18,38,0.55) 60%, rgba(10,18,38,0.75) 100%);
  }}
  .photo-badge .role {{
    font-size: 10px;
    letter-spacing: 0.38em;
    color: #c4a15a;
    text-transform: uppercase;
    font-weight: 600;
    margin-bottom: 4px;
  }}
  .photo-badge .name {{
    font-family: 'Playfair Display', serif;
    font-style: italic;
    font-size: 20px;
    color: #f5f1e6;
    font-weight: 500;
    letter-spacing: 0.01em;
  }}
</style>
</head>
<body>
  <div class="banner">
    <div class="content">
      <div class="eyebrow">
        <span class="line"></span>
        <span>Oferta Exclusiva de Agosto</span>
        <span class="line"></span>
      </div>

      <h1 class="headline">
        ANIVERSÁRIO DE 7 ANOS <span class="dot">·</span> <span class="accent">CURSOS ONLINE</span>
      </h1>

      <p class="subline">
        Curso de Radiestesia Aplicada a Mesas Radiônicas Quânticas
      </p>

      <div class="offer">
        <p class="offer-text">
          Quem faz a inscrição este mês ganha, além dos bônus que já recebe,
          <span class="kit">+ 1 KIT</span>
          de Mesas Radiônicas Quânticas
        </p>
      </div>

      <div class="bottom-row">
        <div class="price">
          <span class="from">R$ 1.397</span>
          <span class="by">por</span>
          <span class="value"><span class="rs">R$</span>890</span>
          <span class="install">ou 12x R$ 92,05</span>
        </div>
        <a class="cta">Quero garantir minha vaga</a>
      </div>

      <div class="footer">
        Até 31 de agosto <span class="sep">·</span> Garantia de 7 dias
      </div>
    </div>

    <div class="photo-badge">
      <div class="role">Terapeuta &middot; Autor</div>
      <div class="name">Prof. Francisco Borrello</div>
    </div>

    <div class="photo-col">
      <div class="photo-glow"></div>
      <div class="photo-frame">
        <img src="{fran_data}" alt="Prof. Francisco Borrello">
      </div>
    </div>
  </div>
</body>
</html>
"""

def render():
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page(viewport={"width": 1920, "height": 600}, device_scale_factor=1)
        page.set_content(HTML, wait_until="networkidle")
        page.wait_for_timeout(400)
        page.screenshot(path=str(OUT_PNG), clip={"x": 0, "y": 0, "width": 1920, "height": 600}, type="png")
        page.screenshot(path=str(OUT_JPG), clip={"x": 0, "y": 0, "width": 1920, "height": 600}, type="jpeg", quality=92)
        browser.close()
    print(f"OK -> {OUT_PNG}")
    print(f"OK -> {OUT_JPG}")

if __name__ == "__main__":
    render()
