#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Gera PDF de 2 slides — Pitch Feng Shui + Promo Loja — Prof. Francisco Borrello

Slide 1: pitch do curso (HTML/WeasyPrint - visual elegante)
Slide 2: promo da loja (ReportLab Canvas - controle absoluto de posicionamento)

Ambos em 960x540 pontos (proporcao 16:9, escala do WeasyPrint 96 DPI -> 72 DPI).
Depois os dois PDFs sao unidos em um so.
"""

from weasyprint import HTML
from reportlab.pdfgen import canvas
from reportlab.lib.colors import HexColor
from pypdf import PdfWriter, PdfReader
from pathlib import Path

OUT_DIR = Path("/opt/mia/workspace/clientes/borrello/campanha_mesas_agosto2026")
OUT_PDF = OUT_DIR / "pitch_feng_shui_dia_dos_pais.pdf"
TMP_SLIDE1 = OUT_DIR / "_tmp_slide1.pdf"
TMP_SLIDE2 = OUT_DIR / "_tmp_slide2.pdf"

# Dimensoes finais em pontos (PDF)
# WeasyPrint converte px -> pt em 96 DPI: 1280px = 960pt, 720px = 540pt
W, H = 960, 540

# ============================================================
# SLIDE 1 — HTML/WeasyPrint (compactado para caber em UMA pagina)
# ============================================================

HTML_SLIDE1 = r"""<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<style>
  @import url('https://fonts.googleapis.com/css2?family=Cormorant+Garamond:ital,wght@0,300;0,400;0,500;0,600;0,700;1,400&family=Inter:wght@300;400;500;600;700&display=swap');

  @page { size: 1280px 720px; margin: 0; }
  * { box-sizing: border-box; margin: 0; padding: 0; }

  html, body {
    width: 1280px; height: 720px;
    font-family: 'Inter', sans-serif;
    color: #f5e6c4;
    background: #05050a;
    -webkit-font-smoothing: antialiased;
  }

  .slide {
    width: 1280px; height: 720px;
    position: relative; overflow: hidden;
    background:
      radial-gradient(ellipse at 20% 15%, rgba(212, 166, 86, 0.14) 0%, transparent 45%),
      radial-gradient(ellipse at 85% 90%, rgba(180, 120, 60, 0.10) 0%, transparent 45%),
      radial-gradient(ellipse at 50% 50%, rgba(20, 15, 30, 0.5) 0%, transparent 70%),
      linear-gradient(140deg, #0a0a14 0%, #12101c 50%, #08060f 100%);
  }

  .bagua {
    position: absolute; width: 520px; height: 520px;
    border: 1px solid rgba(212, 166, 86, 0.18);
    border-radius: 50%; right: -180px; top: -140px;
  }
  .bagua::before {
    content: ""; position: absolute; inset: 40px;
    border: 1px solid rgba(212, 166, 86, 0.12); border-radius: 50%;
  }
  .bagua::after {
    content: ""; position: absolute; inset: 90px;
    border: 1px dashed rgba(212, 166, 86, 0.10); border-radius: 50%;
  }
  .bagua-bl {
    position: absolute; width: 380px; height: 380px;
    border: 1px solid rgba(212, 166, 86, 0.12);
    border-radius: 50%; left: -160px; bottom: -160px;
  }

  .frame {
    position: absolute; inset: 22px;
    border: 1px solid rgba(212, 166, 86, 0.35);
  }
  .frame-corners span {
    position: absolute; width: 40px; height: 40px; border: 2px solid #d4a656;
  }
  .frame-corners .tl { top: 16px; left: 16px; border-right: none; border-bottom: none; }
  .frame-corners .tr { top: 16px; right: 16px; border-left: none; border-bottom: none; }
  .frame-corners .bl { bottom: 16px; left: 16px; border-right: none; border-top: none; }
  .frame-corners .br { bottom: 16px; right: 16px; border-left: none; border-top: none; }

  .content {
    position: relative; z-index: 5;
    padding: 50px 80px; height: 100%;
    display: flex; flex-direction: column;
  }

  .brand {
    display: flex; align-items: center; gap: 14px;
    font-family: 'Cormorant Garamond', serif;
    font-size: 14px; letter-spacing: 4px;
    text-transform: uppercase; color: #d4a656; font-weight: 500;
  }
  .brand .dot {
    width: 8px; height: 8px; background: #d4a656;
    border-radius: 50%;
  }
  .brand .line {
    flex: 0 0 50px; height: 1px;
    background: linear-gradient(90deg, #d4a656, transparent);
  }

  .eyebrow {
    margin-top: 22px;
    font-family: 'Cormorant Garamond', serif;
    font-style: italic; font-size: 20px;
    color: #b89968; letter-spacing: 1px;
  }

  h1.headline {
    font-family: 'Cormorant Garamond', serif;
    font-weight: 500; font-size: 48px; line-height: 1.05;
    color: #f5e6c4; margin-top: 8px;
    max-width: 900px; letter-spacing: -0.5px;
  }
  h1.headline .gold {
    color: #d4a656; font-style: italic; font-weight: 600;
  }

  .sub {
    margin-top: 14px; font-size: 16px;
    color: #cfc3a6; max-width: 900px;
    line-height: 1.45; font-weight: 300;
  }

  .divider {
    margin-top: 18px; width: 100px; height: 1px;
    background: linear-gradient(90deg, #d4a656 0%, transparent 100%);
  }

  .bullets {
    margin-top: 16px; display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px 40px; max-width: 1000px;
  }
  .bullets li {
    list-style: none; font-size: 14px;
    color: #d8ccae; padding-left: 20px;
    position: relative; line-height: 1.4; font-weight: 300;
  }
  .bullets li::before {
    content: ""; position: absolute;
    left: 0; top: 6px; width: 7px; height: 7px;
    border: 1px solid #d4a656; transform: rotate(45deg);
  }

  .offer {
    margin-top: auto; display: flex;
    align-items: center; justify-content: space-between;
    gap: 40px; padding: 20px 32px;
    background: linear-gradient(135deg, rgba(212, 166, 86, 0.10), rgba(212, 166, 86, 0.03));
    border: 1px solid rgba(212, 166, 86, 0.4);
    border-radius: 4px;
  }
  .offer-left { flex: 1; }
  .offer-label {
    font-family: 'Cormorant Garamond', serif;
    font-style: italic; color: #b89968;
    font-size: 15px; letter-spacing: 2px;
    text-transform: uppercase; font-weight: 500;
  }
  .offer-price {
    display: flex; align-items: baseline;
    gap: 18px; margin-top: 6px;
  }
  .price-old {
    font-family: 'Cormorant Garamond', serif;
    font-size: 22px; color: #7a7364;
    text-decoration: line-through; text-decoration-color: #a8391f;
  }
  .price-new {
    font-family: 'Cormorant Garamond', serif;
    font-weight: 700; font-size: 54px;
    color: #f5e6c4; line-height: 1;
  }
  .price-new .cur {
    font-size: 26px; color: #d4a656;
    margin-right: 4px; vertical-align: super;
  }
  .offer-limit {
    margin-top: 4px; font-size: 13px;
    color: #cfc3a6; font-weight: 400;
  }
  .offer-limit b { color: #d4a656; }

  .cta {
    background: linear-gradient(135deg, #d4a656 0%, #b78642 100%);
    color: #0a0a14; padding: 16px 24px;
    font-family: 'Inter', sans-serif;
    font-weight: 700; font-size: 12px;
    letter-spacing: 3px; text-transform: uppercase;
    border-radius: 2px;
    text-align: center; max-width: 220px; line-height: 1.3;
  }
</style>
</head>
<body>
<section class="slide">
  <div class="bagua"></div>
  <div class="bagua-bl"></div>
  <div class="frame"></div>
  <div class="frame-corners"><span class="tl"></span><span class="tr"></span><span class="bl"></span><span class="br"></span></div>

  <div class="content">
    <div class="brand">
      <span class="dot"></span>
      <span>Prof. Francisco Borrello</span>
      <span class="line"></span>
      <span style="letter-spacing:2px; font-size:12px; color:#b89968;">35 anos de pratica</span>
    </div>

    <div class="eyebrow">Curso Feng Shui - Chapeu Preto &amp; 5 Animais Celestiais</div>

    <h1 class="headline">
      Transforme a <span class="gold">energia da sua casa</span><br>
      - e da sua vida.
    </h1>

    <p class="sub">
      Aprenda a mapear os 9 cantos do Bagua e ler a paisagem pelos 5 Animais Celestiais.
      Duas escolas milenares reunidas num metodo pratico para atrair prosperidade, saude e bons relacionamentos.
    </p>

    <div class="divider"></div>

    <ul class="bullets">
      <li>Bagua do Chapeu Preto: 9 areas da vida mapeadas no seu ambiente</li>
      <li>5 Animais Celestiais: leitura de elementos e formas da paisagem</li>
      <li>Como identificar e corrigir bloqueios energeticos do lar</li>
      <li>Posicionamento de moveis, cores, agua, plantas e objetos</li>
      <li>Tecnicas praticas aplicaveis ja na primeira semana</li>
      <li>Ambiente alinhado com seus objetivos concretos de vida</li>
    </ul>

    <div class="offer">
      <div class="offer-left">
        <div class="offer-label">Oferta Especial - Dia dos Pais</div>
        <div class="offer-price">
          <span class="price-old">De R$ 1.497</span>
          <span class="price-new"><span class="cur">R$</span>617</span>
        </div>
        <div class="offer-limit">Valido <b>somente ate domingo</b> - Dia dos Pais</div>
      </div>
      <div class="cta">Garanta agora<br>com desconto</div>
    </div>
  </div>
</section>
</body>
</html>
"""

# ============================================================
# SLIDE 2 — ReportLab Canvas em 960x540pt
# ============================================================

BG_DARK      = HexColor("#0a0a14")
BG_DARK2     = HexColor("#12101c")
GOLD         = HexColor("#d4a656")
GOLD_DIM     = HexColor("#b89968")
CREAM        = HexColor("#f5e6c4")
CREAM_DIM    = HexColor("#cfc3a6")
CARD_BG      = HexColor("#15121f")


def draw_slide2(c: canvas.Canvas):
    # ---- FUNDO ----
    c.setFillColor(BG_DARK)
    c.rect(0, 0, W, H, fill=1, stroke=0)
    c.setFillColor(BG_DARK2)
    c.setFillAlpha(0.6)
    c.rect(0, 0, W, H, fill=1, stroke=0)
    c.setFillAlpha(1)

    # ---- ORNAMENTOS BAGUA ----
    c.setStrokeColor(GOLD)
    c.setLineWidth(0.8)
    c.setStrokeAlpha(0.18)
    c.circle(W + 20, H + 20, 260, stroke=1, fill=0)
    c.setStrokeAlpha(0.12)
    c.circle(W + 20, H + 20, 210, stroke=1, fill=0)
    c.setStrokeAlpha(0.12)
    c.circle(-30, -30, 200, stroke=1, fill=0)
    c.setStrokeAlpha(1)

    # ---- MOLDURA ----
    c.setStrokeColor(GOLD)
    c.setStrokeAlpha(0.35)
    c.setLineWidth(0.8)
    c.rect(18, 18, W - 36, H - 36, stroke=1, fill=0)
    c.setStrokeAlpha(1)

    # ---- CANTOS DOURADOS ----
    corner_len = 30
    c.setStrokeColor(GOLD)
    c.setLineWidth(1.5)
    for cx, cy, dx, dy in [
        (12, H - 12, 1, -1),      # top-left
        (W - 12, H - 12, -1, -1), # top-right
        (12, 12, 1, 1),           # bottom-left
        (W - 12, 12, -1, 1),      # bottom-right
    ]:
        c.line(cx, cy, cx + dx * corner_len, cy)
        c.line(cx, cy, cx, cy + dy * corner_len)

    # Helper para pensar em coordenadas do topo
    def yt(y_from_top):
        return H - y_from_top

    left_x = 60

    # ---- HEADER: brand ----
    c.setFillColor(GOLD)
    c.circle(left_x + 4, yt(52), 3, fill=1, stroke=0)
    c.setFillColor(GOLD)
    c.setFont("Helvetica-Bold", 10)
    c.drawString(left_x + 15, yt(55), "LOJA PROF. FRANCISCO BORRELLO")
    c.setStrokeColor(GOLD)
    c.setStrokeAlpha(0.5)
    c.setLineWidth(0.8)
    c.line(left_x + 245, yt(53), left_x + 295, yt(53))
    c.setStrokeAlpha(1)
    c.setFillColor(GOLD_DIM)
    c.setFont("Helvetica", 9)
    c.drawString(left_x + 305, yt(55), "RADIESTESIA & FENG SHUI")

    # ---- HEADLINE ----
    c.setFillColor(CREAM)
    c.setFont("Helvetica-Bold", 36)
    c.drawString(left_x, yt(110), "Promocao Dia dos Pais")

    # subtitulo
    c.setFillColor(GOLD_DIM)
    c.setFont("Helvetica-Oblique", 15)
    c.drawString(left_x, yt(135), "Duas formas de aproveitar. Escolha a que combina com voce.")

    # divisor
    c.setStrokeColor(GOLD)
    c.setLineWidth(1)
    c.line(left_x, yt(150), left_x + 100, yt(150))

    # ============ CARDS ============
    # Espaco disponivel para cards: yt(170) ate yt(360) = ~190px de altura
    card_w = 340
    card_h = 190
    card_gap = 60
    total_w = card_w * 2 + card_gap
    card1_x = (W - total_w) / 2
    card2_x = card1_x + card_w + card_gap
    card_y = yt(170) - card_h  # y do reportlab (origem inferior)

    def draw_card(x, y, w, h, badge, big_number, desc, fine):
        # fundo
        c.setFillColor(CARD_BG)
        c.setStrokeColor(GOLD)
        c.setLineWidth(1.3)
        c.roundRect(x, y, w, h, 6, fill=1, stroke=1)

        # borda interna sutil
        c.setStrokeColor(GOLD)
        c.setStrokeAlpha(0.2)
        c.setLineWidth(0.5)
        c.roundRect(x + 5, y + 5, w - 10, h - 10, 4, fill=0, stroke=1)
        c.setStrokeAlpha(1)

        cx = x + w / 2

        # badge
        c.setFillColor(GOLD_DIM)
        c.setFont("Helvetica-Oblique", 12)
        c.drawCentredString(cx, y + h - 32, badge)

        # linha divisoria abaixo do badge
        c.setStrokeColor(GOLD)
        c.setStrokeAlpha(0.5)
        c.setLineWidth(0.6)
        c.line(cx - 25, y + h - 42, cx + 25, y + h - 42)
        c.setStrokeAlpha(1)

        # numero gigante + %
        c.setFillColor(GOLD)
        num_size = 70
        pct_size = 34
        c.setFont("Helvetica-Bold", num_size)
        num_w = c.stringWidth(big_number, "Helvetica-Bold", num_size)
        pct_w = c.stringWidth("%", "Helvetica-Bold", pct_size)
        combined_w = num_w + pct_w + 3
        num_x = cx - combined_w / 2
        num_y = y + h - 120
        c.drawString(num_x, num_y, big_number)
        c.setFont("Helvetica-Bold", pct_size)
        c.drawString(num_x + num_w + 3, num_y + 34, "%")

        # desc
        c.setFillColor(CREAM)
        c.setFont("Helvetica-Bold", 16)
        c.drawCentredString(cx, y + 38, desc)

        # letra miuda
        c.setFillColor(HexColor("#a89b7d"))
        c.setFont("Helvetica", 10)
        c.drawCentredString(cx, y + 20, fine)

    draw_card(
        card1_x, card_y, card_w, card_h,
        badge="PAGAMENTO A VISTA",
        big_number="20",
        desc="de CASHBACK",
        fine="devolvido para voce comprar de novo",
    )

    draw_card(
        card2_x, card_y, card_w, card_h,
        badge="CARTAO EM 3X",
        big_number="12",
        desc="de DESCONTO",
        fine="parcelado sem juros no cartao",
    )

    # ---- "OU" entre os cards ----
    ou_x = card1_x + card_w + card_gap / 2
    ou_y = card_y + card_h / 2
    c.setFillColor(BG_DARK)
    c.setStrokeColor(GOLD)
    c.setLineWidth(1)
    c.circle(ou_x, ou_y, 18, fill=1, stroke=1)
    c.setFillColor(GOLD)
    c.setFont("Helvetica-Oblique", 15)
    c.drawCentredString(ou_x, ou_y - 5, "ou")

    # ============ SECAO PRODUTOS INCLUIDOS ============
    info_y_top = yt(390)
    info_h = 90
    info_x = left_x
    info_w = W - left_x * 2
    info_y = info_y_top - info_h

    c.setFillColor(HexColor("#0a0a14"))
    c.setStrokeColor(GOLD)
    c.setStrokeAlpha(0.4)
    c.setLineWidth(1)
    c.rect(info_x, info_y, info_w, info_h, fill=1, stroke=1)
    c.setStrokeAlpha(1)

    # barra dourada esquerda
    c.setFillColor(GOLD)
    c.rect(info_x, info_y, 4, info_h, fill=1, stroke=0)

    # label
    c.setFillColor(GOLD_DIM)
    c.setFont("Helvetica-Oblique", 11)
    c.drawString(info_x + 20, info_y + info_h - 22, "VALIDO PARA")

    # itens
    c.setFillColor(CREAM)
    c.setFont("Helvetica-Bold", 14)
    c.drawString(info_x + 20, info_y + info_h - 46,
                 "Kits para projetos de Radiestesia com pendulo")
    c.drawString(info_x + 20, info_y + info_h - 68,
                 "Itens de Feng Shui da loja")

    # ============ ASSINATURA ============
    sig_y = yt(510)
    c.setFillColor(GOLD)
    c.setFont("Helvetica-Bold", 10)
    c.drawCentredString(W / 2, sig_y,
                        "PROF. FRANCISCO BORRELLO   -   DIA DOS PAIS 2026")


def gerar_slide2():
    c = canvas.Canvas(str(TMP_SLIDE2), pagesize=(W, H))
    draw_slide2(c)
    c.showPage()
    c.save()


def gerar_slide1():
    HTML(string=HTML_SLIDE1, base_url=str(OUT_DIR)).write_pdf(str(TMP_SLIDE1))


def unir_pdfs():
    writer = PdfWriter()
    for path in [TMP_SLIDE1, TMP_SLIDE2]:
        reader = PdfReader(str(path))
        # so pega a primeira pagina de cada, evita overflow acidental
        writer.add_page(reader.pages[0])
    with open(OUT_PDF, "wb") as f:
        writer.write(f)


def limpar_temp():
    for p in [TMP_SLIDE1, TMP_SLIDE2]:
        if p.exists():
            p.unlink()


def main():
    OUT_DIR.mkdir(parents=True, exist_ok=True)
    gerar_slide1()
    gerar_slide2()
    unir_pdfs()
    limpar_temp()

    size_kb = OUT_PDF.stat().st_size / 1024
    print(f"OK {OUT_PDF} ({size_kb:.1f} KB)")


if __name__ == "__main__":
    main()
