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_v3_mobile.png")
OUT_JPG = Path("/opt/mia/workspace/clientes/borrello/banner_mesas_radionicas_v3_mobile.jpg")

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

# MOBILE 1080x1350 (4:5 - feed Instagram)
# HIERARQUIA VERTICAL: eyebrow -> headline -> foto -> subtitulo -> oferta -> preco -> CTA -> footer
# padding lateral 70px, foto ocupa aprox 40% (540px altura)

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: 1080px;
    height: 1350px;
    font-family: 'Inter', sans-serif;
    color: #ecebe4;
  }}

  .banner {{
    position: relative;
    width: 1080px;
    height: 1350px;
    background:
      radial-gradient(700px 700px at 50% 38%, rgba(196,161,90,0.22) 0%, rgba(196,161,90,0) 65%),
      radial-gradient(900px 700px at 50% 80%, rgba(20,32,60,0.9) 0%, rgba(10,18,38,0.9) 60%),
      linear-gradient(180deg, #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: 30px; left: 30px; right: 30px; bottom: 30px;
    border: 1px solid rgba(196,161,90,0.22);
    pointer-events: none;
    z-index: 4;
  }}

  .container {{
    position: relative;
    width: 1080px;
    height: 1350px;
    padding: 60px 70px 60px 70px;
    z-index: 2;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
  }}

  /* EYEBROW - topo */
  .eyebrow {{
    font-family: 'Inter', sans-serif;
    font-size: 18px;
    letter-spacing: 0.42em;
    text-transform: uppercase;
    color: #c4a15a;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 18px;
    margin-top: 6px;
  }}
  .eyebrow .line {{
    width: 60px; height: 1px; background: #c4a15a; opacity: 0.7;
  }}

  /* FOTO - central, grande */
  .photo-block {{
    position: relative;
    width: 100%;
    height: 500px;
    margin-top: 22px;
    margin-bottom: 6px;
    display: flex;
    align-items: flex-end;
    justify-content: center;
  }}
  .photo-glow {{
    position: absolute;
    bottom: -20px;
    left: 50%;
    transform: translateX(-50%);
    width: 500px;
    height: 500px;
    background: radial-gradient(circle, rgba(196,161,90,0.35) 0%, rgba(196,161,90,0) 65%);
    filter: blur(30px);
    z-index: 1;
  }}
  .photo-block img {{
    position: relative;
    z-index: 2;
    height: 500px;
    width: auto;
    max-width: 480px;
    object-fit: contain;
    object-position: bottom center;
    filter: drop-shadow(0 24px 44px rgba(0,0,0,0.6));
  }}

  /* HEADLINE - abaixo da foto */
  .headline {{
    font-family: 'Playfair Display', serif;
    font-weight: 700;
    font-size: 54px;
    line-height: 1.06;
    letter-spacing: 0.01em;
    color: #f5f1e6;
    margin-top: 18px;
    max-width: 900px;
  }}
  .headline .dot {{
    color: #c4a15a;
    margin: 0 8px;
    font-weight: 400;
  }}
  .headline .accent {{
    color: #c4a15a;
    font-style: italic;
    font-weight: 600;
    display: block;
    margin-top: 4px;
  }}

  /* SUBLINE */
  .subline {{
    font-family: 'Cormorant Garamond', serif;
    font-style: italic;
    font-weight: 500;
    font-size: 32px;
    color: #d6d1c1;
    line-height: 1.22;
    max-width: 880px;
    margin-top: 16px;
  }}

  /* OFERTA */
  .offer {{
    background: linear-gradient(135deg, rgba(196,161,90,0.14) 0%, rgba(196,161,90,0.04) 100%);
    border: 1px solid rgba(196,161,90,0.42);
    padding: 22px 30px;
    border-radius: 5px;
    max-width: 900px;
    margin-top: 20px;
  }}
  .offer-text {{
    font-family: 'Cormorant Garamond', serif;
    font-size: 26px;
    line-height: 1.32;
    color: #ecebe4;
    font-weight: 500;
  }}
  .offer-text .kit {{
    font-family: 'Playfair Display', serif;
    font-weight: 800;
    font-size: 40px;
    color: #d9b56a;
    letter-spacing: 0.02em;
    text-shadow: 0 0 22px rgba(217,181,106,0.4);
    display: inline-block;
    padding: 0 4px;
  }}

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

  /* CTA */
  .cta {{
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 22px 56px;
    background: linear-gradient(135deg, #d9b56a 0%, #b8944a 100%);
    color: #0a1226;
    font-family: 'Inter', sans-serif;
    font-weight: 800;
    font-size: 20px;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    border-radius: 4px;
    box-shadow: 0 8px 28px rgba(217,181,106,0.34);
    border: 1px solid #e6c684;
    white-space: nowrap;
    margin-top: 18px;
  }}

  .footer {{
    font-family: 'Inter', sans-serif;
    font-size: 14px;
    color: #8a8778;
    letter-spacing: 0.28em;
    text-transform: uppercase;
    margin-top: 16px;
  }}
  .footer .sep {{
    color: #c4a15a;
    margin: 0 12px;
  }}
</style>
</head>
<body>
  <div class="banner">
    <div class="container">
      <div class="eyebrow">
        <span class="line"></span>
        <span>Oferta Exclusiva de Agosto</span>
        <span class="line"></span>
      </div>

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

      <h1 class="headline">
        ANIVERSÁRIO DE 7 ANOS
        <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="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">12x R$ 92,05</span>
      </div>

      <a class="cta">Quero garantir minha vaga</a>

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

def render():
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page(viewport={"width": 1080, "height": 1350}, 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": 1080, "height": 1350}, type="png")
        page.screenshot(path=str(OUT_JPG), clip={"x": 0, "y": 0, "width": 1080, "height": 1350}, type="jpeg", quality=92)
        browser.close()
    print(f"OK -> {OUT_PNG}")
    print(f"OK -> {OUT_JPG}")

if __name__ == "__main__":
    render()
