#!/usr/bin/env python3
"""
Anuncio de lancamento 1:1 - Curso Cromoterapia Estelar - Francisco Borrello
Data de liberacao: 01/08/2026
"""
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"
OUT_PNG = BASE / "anuncio_lancamento_cromoterapia.png"
OUT_JPG = BASE / "anuncio_lancamento_cromoterapia.jpg"

FONTS = Path("/home/mia/.fonts/px3lab")

def font_b64(name):
    return base64.b64encode((FONTS / name).read_bytes()).decode()

bebas = font_b64("BebasNeue-Regular.ttf")
inter_bold = font_b64("Inter-Bold.ttf")
inter_reg = font_b64("Inter-Regular.ttf")
inter_semi = font_b64("Inter-SemiBold.ttf")

bg_b64 = base64.b64encode(BG.read_bytes()).decode()

HTML = f"""<!doctype html>
<html><head><meta charset="utf-8">
<style>
@font-face {{ font-family: 'Bebas'; src: url(data:font/ttf;base64,{bebas}) format('truetype'); }}
@font-face {{ font-family: 'Inter'; font-weight: 400; src: url(data:font/ttf;base64,{inter_reg}) format('truetype'); }}
@font-face {{ font-family: 'Inter'; font-weight: 600; src: url(data:font/ttf;base64,{inter_semi}) format('truetype'); }}
@font-face {{ font-family: 'Inter'; font-weight: 700; src: url(data:font/ttf;base64,{inter_bold}) format('truetype'); }}

* {{ box-sizing: border-box; margin: 0; padding: 0; }}
html, body {{ margin: 0; padding: 0; overflow: hidden; width: 1080px; height: 1080px; }}
body {{
  width: 1080px; height: 1080px;
  font-family: 'Inter', sans-serif;
  color: #fff;
  position: relative;
  background: #05060d;
}}

.bg {{
  position: absolute; inset: 0;
  background-image: url(data:image/png;base64,{bg_b64});
  background-size: cover;
  background-position: center top;
  filter: saturate(1.15) contrast(1.05);
}}

/* overlay escuro + tinta violeta para legibilidade e clima espiritual */
.overlay {{
  position: absolute; inset: 0;
  background:
    radial-gradient(ellipse at 50% 8%, rgba(255,200,90,0.28) 0%, rgba(255,200,90,0) 45%),
    radial-gradient(ellipse at 50% 55%, rgba(80,40,140,0.35) 0%, rgba(10,8,25,0) 70%),
    linear-gradient(180deg, rgba(6,8,20,0.55) 0%, rgba(6,8,20,0.72) 55%, rgba(4,5,15,0.92) 100%);
}}

/* borda dourada fina interna */
.frame {{
  position: absolute; inset: 28px;
  border: 1.5px solid rgba(212, 175, 90, 0.35);
  border-radius: 4px;
  pointer-events: none;
}}
.frame::before, .frame::after {{
  content: ''; position: absolute; width: 42px; height: 42px;
  border: 2px solid #d4af5a;
}}
.frame::before {{ top: -1px; left: -1px; border-right: none; border-bottom: none; }}
.frame::after {{ bottom: -1px; right: -1px; border-left: none; border-top: none; }}
.frame-tr, .frame-bl {{
  position: absolute; width: 42px; height: 42px; border: 2px solid #d4af5a;
}}
.frame-tr {{ top: 27px; right: 27px; border-left: none; border-bottom: none; }}
.frame-bl {{ bottom: 27px; left: 27px; border-right: none; border-top: none; }}

.content {{
  position: relative; z-index: 3;
  width: 100%; height: 100%;
  display: flex; flex-direction: column;
  align-items: center; justify-content: space-between;
  padding: 100px 80px 80px;
  text-align: center;
}}

/* Topo - selo */
.top {{
  display: flex; flex-direction: column; align-items: center; gap: 18px;
}}
.selo {{
  font-family: 'Inter', sans-serif; font-weight: 600;
  font-size: 13px; letter-spacing: 6px; text-transform: uppercase;
  color: #d4af5a;
  padding: 10px 26px;
  border: 1px solid rgba(212, 175, 90, 0.55);
  border-radius: 100px;
  background: rgba(20, 15, 40, 0.35);
}}
.eyebrow {{
  font-family: 'Inter', sans-serif; font-weight: 600;
  font-size: 15px; letter-spacing: 8px; text-transform: uppercase;
  color: #e8d9a8;
  margin-top: 4px;
}}

/* Centro - data */
.center {{
  display: flex; flex-direction: column; align-items: center;
  margin-top: -20px;
}}
.date-block {{
  display: flex; flex-direction: column; align-items: center;
  position: relative;
}}
.date-block::before, .date-block::after {{
  content: ''; position: absolute; top: 50%;
  width: 140px; height: 1px;
  background: linear-gradient(90deg, transparent 0%, #d4af5a 50%, transparent 100%);
}}
.date-block::before {{ left: -180px; }}
.date-block::after {{ right: -180px; }}

.date {{
  font-family: 'Bebas', sans-serif;
  font-size: 168px;
  line-height: 0.9;
  letter-spacing: 2px;
  background: linear-gradient(180deg, #fbe89f 0%, #e6c568 45%, #b98a35 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-shadow: 0 4px 30px rgba(212,175,90,0.35);
  filter: drop-shadow(0 2px 12px rgba(0,0,0,0.5));
}}
.date-full {{
  font-family: 'Bebas', sans-serif;
  font-size: 46px;
  letter-spacing: 14px;
  color: #f5e4b2;
  margin-top: 8px;
}}

/* Nome do curso */
.course {{
  margin-top: 40px;
  max-width: 780px;
}}
.course-title {{
  font-family: 'Bebas', sans-serif;
  font-size: 62px;
  line-height: 1;
  letter-spacing: 2px;
  color: #ffffff;
  text-shadow: 0 2px 20px rgba(0,0,0,0.6);
}}
.course-sub {{
  font-family: 'Inter', sans-serif; font-weight: 600;
  font-size: 22px; letter-spacing: 3px; text-transform: uppercase;
  color: #b9a3d9;
  margin-top: 14px;
}}

/* Descricao */
.desc {{
  max-width: 820px;
  margin-top: 32px;
  font-family: 'Inter', sans-serif; font-weight: 400;
  font-size: 20px;
  line-height: 1.55;
  color: rgba(240, 240, 250, 0.88);
  text-shadow: 0 1px 8px rgba(0,0,0,0.6);
}}
.desc b {{ color: #f5e4b2; font-weight: 700; }}

/* Rodape */
.footer {{
  display: flex; flex-direction: column; align-items: center; gap: 10px;
}}
.divider {{
  width: 60px; height: 1px;
  background: #d4af5a;
  margin-bottom: 4px;
}}
.author {{
  font-family: 'Bebas', sans-serif;
  font-size: 34px; letter-spacing: 6px;
  color: #f5e4b2;
}}
.author-label {{
  font-family: 'Inter', sans-serif; font-weight: 600;
  font-size: 11px; letter-spacing: 5px; text-transform: uppercase;
  color: rgba(212, 175, 90, 0.75);
}}
</style>
</head><body>
  <div class="bg"></div>
  <div class="overlay"></div>
  <div class="frame"></div>
  <div class="frame-tr"></div>
  <div class="frame-bl"></div>

  <div class="content">
    <div class="top">
      <div class="selo">Lançamento Oficial</div>
      <div class="eyebrow">Conteúdo disponível a partir de</div>
    </div>

    <div class="center">
      <div class="date-block">
        <div class="date">01 DE AGOSTO</div>
        <div class="date-full">2 0 2 6</div>
      </div>

      <div class="course">
        <div class="course-title">Cromoterapia Estelar<br>das Luzes Angelicais</div>
        <div class="course-sub">com Radiestesia Passo a Passo</div>
      </div>

      <div class="desc">
        O único método que une o <b>diagnóstico energético preciso da Radiestesia</b>
        com o <b>poder transformador da Cromoterapia em três camadas</b>.
        Aprenda a identificar e tratar desequilíbrios no corpo físico,
        emocional e espiritual - do zero até a prática completa.
      </div>
    </div>

    <div class="footer">
      <div class="divider"></div>
      <div class="author-label">PROFESSOR</div>
      <div class="author">FRANCISCO BORRELLO</div>
    </div>
  </div>
</body></html>"""

def render():
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page(viewport={"width": 1080, "height": 1080}, device_scale_factor=2)
        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": 1080}, omit_background=False)
        browser.close()
    # Converter para JPG qualidade 94
    from PIL import Image
    img = Image.open(OUT_PNG).convert("RGB")
    if img.size != (1080, 1080):
        img = img.resize((1080, 1080), Image.LANCZOS)
    img.save(OUT_JPG, "JPEG", quality=94, optimize=True, progressive=True)
    print(f"OK -> {OUT_JPG} ({OUT_JPG.stat().st_size // 1024} KB)")

if __name__ == "__main__":
    render()
