"""
Criativos 02-10 v2 - Chácara Sonho Verde Top
Mantém a mesma hierarquia visual do v2 aprovado (01):
  - Foto grande topo com overlay
  - Badge topo (contexto do público)
  - Ornament (linha + losango + linha)
  - Headline Playfair Black
  - LOCAL destacado (Valinhos-SP · 80 km de SP) com pin dourado e divisória
  - Sub-headline citando hospedagem para 65 pessoas
  - CTA pill dourado + brand rodapé
"""
import base64
import sys
from pathlib import Path
from playwright.sync_api import sync_playwright

BASE = Path('/opt/mia/workspace/clientes/chacara-sonho-verde')
FOTOS = BASE / 'fotos_originais'
OUT = BASE


def img_to_data_uri(path: Path) -> str:
    b64 = base64.b64encode(path.read_bytes()).decode()
    return f"data:image/jpeg;base64,{b64}"


def build_html(foto_path: Path, badge: str, headline_html: str, sub_html: str, cta: str) -> str:
    foto_uri = img_to_data_uri(foto_path)
    return f"""
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="utf-8">
<link href="https://fonts.googleapis.com/css2?family=Playfair+Display:ital,wght@0,400;0,700;0,900;1,400&family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
<style>
  * {{ margin: 0; padding: 0; box-sizing: border-box; }}
  body {{ margin: 0; overflow: hidden; background: #0d1f14; font-family: 'Inter', sans-serif; }}
  .canvas {{
    width: 1080px; height: 1350px; position: relative; overflow: hidden;
    background: #0d1f14;
  }}
  .hero {{
    position: absolute; top: 0; left: 0; width: 100%; height: 780px;
    background-image: url('{foto_uri}');
    background-size: cover; background-position: center;
    filter: brightness(0.85) contrast(1.05) saturate(1.1);
  }}
  .hero-overlay {{
    position: absolute; top: 0; left: 0; width: 100%; height: 780px;
    background:
      linear-gradient(180deg, rgba(13,31,20,0.55) 0%, rgba(13,31,20,0.1) 25%, rgba(13,31,20,0.2) 55%, rgba(13,31,20,0.9) 92%, #0d1f14 100%);
  }}
  .top-badge {{
    position: absolute; top: 40px; left: 50%; transform: translateX(-50%);
    display: flex; align-items: center; gap: 10px;
    padding: 10px 22px;
    background: rgba(255,255,255,0.08);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255,255,255,0.2);
    border-radius: 100px;
    color: #f8f6f0;
    font-family: 'Inter', sans-serif;
    font-size: 14px; font-weight: 500; letter-spacing: 3px; text-transform: uppercase;
    z-index: 3;
    white-space: nowrap;
  }}
  .top-badge::before {{
    content: ''; width: 6px; height: 6px; background: #ffc107; border-radius: 50%;
    box-shadow: 0 0 8px #ffc107;
  }}
  .content {{
    position: absolute; bottom: 0; left: 0; width: 100%; height: 720px;
    padding: 0 70px 55px 70px;
    display: flex; flex-direction: column; justify-content: flex-end;
    background: linear-gradient(180deg, transparent 0%, #0d1f14 30%, #0d1f14 100%);
    z-index: 2;
  }}
  .ornament {{
    display: flex; align-items: center; gap: 14px;
    margin-bottom: 22px;
  }}
  .ornament .line {{ height: 1px; width: 60px; background: #ffc107; }}
  .ornament .dot {{ width: 5px; height: 5px; background: #ffc107; transform: rotate(45deg); }}

  .headline {{
    font-family: 'Playfair Display', serif;
    font-weight: 900;
    font-size: 66px;
    line-height: 1.02;
    color: #f8f6f0;
    letter-spacing: -1.5px;
    margin-bottom: 26px;
    text-shadow: 0 6px 30px rgba(0,0,0,0.5);
  }}
  .headline em {{
    font-style: italic; font-weight: 400;
    color: #ffc107;
  }}

  .location {{
    display: flex; align-items: center; gap: 18px;
    margin-bottom: 28px;
    padding-bottom: 26px;
    border-bottom: 1px solid rgba(255,193,7,0.25);
  }}
  .location .pin {{
    display: inline-flex; align-items: center; justify-content: center;
    width: 40px; height: 40px; flex-shrink: 0;
    background: #ffc107; border-radius: 50%;
    box-shadow: 0 4px 20px rgba(255,193,7,0.35);
  }}
  .location .pin svg {{ width: 22px; height: 22px; }}
  .location .text {{
    font-family: 'Inter', sans-serif;
    font-weight: 800;
    font-size: 44px;
    line-height: 1;
    color: #ffc107;
    letter-spacing: -0.5px;
    text-transform: uppercase;
  }}
  .location .text .sep {{
    color: #f8f6f0; font-weight: 300; margin: 0 12px; opacity: 0.6;
  }}
  .location .text .distance {{
    color: #f8f6f0;
  }}

  .sub {{
    font-family: 'Inter', sans-serif;
    font-weight: 400;
    font-size: 22px;
    line-height: 1.45;
    color: #d8e0d3;
    margin-bottom: 38px;
    max-width: 800px;
  }}
  .sub strong {{ color: #f8f6f0; font-weight: 600; }}

  .cta-row {{
    display: flex; align-items: center; justify-content: space-between;
    padding-top: 28px;
    border-top: 1px solid rgba(255,255,255,0.15);
    gap: 20px;
  }}
  .cta {{
    display: inline-flex; align-items: center; gap: 12px;
    padding: 18px 30px;
    background: #ffc107;
    color: #0d1f14;
    font-family: 'Inter', sans-serif;
    font-weight: 700; font-size: 15px;
    letter-spacing: 1.2px; text-transform: uppercase;
    border-radius: 100px;
    box-shadow: 0 10px 30px rgba(255,193,7,0.25);
    white-space: nowrap;
  }}
  .cta .arrow {{
    display: inline-flex; width: 22px; height: 22px; border-radius: 50%;
    background: #0d1f14; color: #ffc107; align-items: center; justify-content: center;
    font-size: 14px;
  }}
  .brand {{ text-align: right; }}
  .brand .logo {{
    font-family: 'Playfair Display', serif;
    font-size: 20px; font-weight: 700; color: #f8f6f0;
    line-height: 1.1;
  }}
  .brand .logo em {{ color: #ffc107; font-style: italic; font-weight: 400; }}
  .brand .phone {{
    font-family: 'Inter', sans-serif;
    font-size: 13px; font-weight: 500; color: #a8d5b5;
    margin-top: 4px; letter-spacing: 0.5px;
  }}
</style>
</head>
<body>
<div class="canvas">
  <div class="hero"></div>
  <div class="hero-overlay"></div>

  <div class="top-badge">{badge}</div>

  <div class="content">
    <div class="ornament">
      <div class="line"></div>
      <div class="dot"></div>
      <div class="line"></div>
    </div>

    <h1 class="headline">{headline_html}</h1>

    <div class="location">
      <span class="pin">
        <svg viewBox="0 0 24 24" fill="#0d1f14"><path d="M12 2C7.58 2 4 5.58 4 10c0 5.25 7 12 8 12s8-6.75 8-12c0-4.42-3.58-8-8-8zm0 11a3 3 0 100-6 3 3 0 000 6z"/></svg>
      </span>
      <div class="text">Valinhos-SP<span class="sep">·</span><span class="distance">80 km de SP</span></div>
    </div>

    <p class="sub">{sub_html}</p>

    <div class="cta-row">
      <div class="cta">
        {cta}
        <span class="arrow">&rarr;</span>
      </div>
      <div class="brand">
        <div class="logo">Chácara <em>Sonho Verde Top</em></div>
        <div class="phone">(11) 94192-3232</div>
      </div>
    </div>
  </div>
</div>
</body>
</html>
"""


CRIATIVOS = [
    {
        "id": "02",
        "foto": "foto_01_treated.jpg",
        "badge": "Confraternização corporativa",
        "headline": "Confraternização pra <em>150 pessoas</em>.<br>Sem restaurante.",
        "sub": "<strong>3 cozinhas + churrasqueira + forno de pizza.</strong><br>Hospedagem para 65 pessoas.",
        "cta": "Chame no WhatsApp",
    },
    {
        "id": "03",
        "foto": "foto_28_treated.jpg",
        "badge": "Retiro de liderança",
        "headline": "Onde líderes tomam<br>as <em>melhores decisões</em>",
        "sub": "<strong>Silêncio, natureza</strong> e hospedagem para 65 pessoas.",
        "cta": "Verifique a data disponível",
    },
    {
        "id": "04",
        "foto": "foto_06_treated.jpg",
        "badge": "Casamento",
        "headline": "Cerimônia, festa e <em>hospedagem</em>.<br>Um só lugar.",
        "sub": "<strong>Seus convidados dormem na chácara.</strong><br>Hospedagem para 65 pessoas.",
        "cta": "Consulte disponibilidade",
    },
    {
        "id": "05",
        "foto": "foto_hero_treated.jpg",
        "badge": "Casamento na serra",
        "headline": "Casamento <em>no alto</em><br>da montanha",
        "sub": "<strong>18 mil m² de reserva privada.</strong><br>Hospedagem para 65 pessoas.",
        "cta": "Reserve sua data",
    },
    {
        "id": "06",
        "foto": "foto_02_treated.jpg",
        "badge": "Salão + área externa",
        "headline": "Salão para <em>120</em>.<br>Área externa sem limite.",
        "sub": "<strong>Seu casamento cresce conforme a lista.</strong><br>Hospedagem para 65 pessoas.",
        "cta": "Chame no WhatsApp",
    },
    {
        "id": "07",
        "foto": "foto_piscina_treated.jpg",
        "badge": "Feriado prolongado",
        "headline": "Feriado com <em>60 amigos</em>.<br>Sem engarrafamento pra praia.",
        "sub": "<strong>Piscina, sauna e trilha a 80 km de SP.</strong><br>Hospedagem para 65 pessoas.",
        "cta": "Consulte disponibilidade",
    },
    {
        "id": "08",
        "foto": "foto_100_treated.jpg",
        "badge": "Família e amigos",
        "headline": "<em>18 mil m²</em> para a<br>família se espalhar",
        "sub": "<strong>Cada um no seu ritmo.</strong><br>Hospedagem para 65 pessoas.",
        "cta": "Chame no WhatsApp",
    },
    {
        "id": "09",
        "foto": "foto_39_treated.jpg",
        "badge": "Tudo em um lugar",
        "headline": "Piscina, sauna e <em>churrasco</em><br>no mesmo lugar",
        "sub": "<strong>Sem sair pra nada.</strong><br>Hospedagem para 65 pessoas.",
        "cta": "Reserve pelo WhatsApp",
    },
    {
        "id": "10",
        "foto": "foto_20_treated.jpg",
        "badge": "Airbnb 5 estrelas",
        "headline": "\"Hotel 5 estrelas\"<br>no <em>coração da mata</em>",
        "sub": "<strong>8 avaliações 5&#9733; no Airbnb.</strong><br>Hospedagem para 65 pessoas.",
        "cta": "Conheça a chácara",
    },
]


def render_one(html: str, dest: Path, w: int = 1080, h: int = 1350):
    with sync_playwright() as p:
        browser = p.chromium.launch()
        page = browser.new_page(viewport={"width": w, "height": h}, device_scale_factor=1)
        page.set_content(html, wait_until="networkidle")
        page.wait_for_timeout(600)
        page.screenshot(path=str(dest), clip={"x": 0, "y": 0, "width": w, "height": h})
        browser.close()


def main(only_ids=None):
    for c in CRIATIVOS:
        if only_ids and c["id"] not in only_ids:
            continue
        foto = FOTOS / c["foto"]
        if not foto.exists():
            print(f"[SKIP {c['id']}] foto nao existe: {foto}")
            continue
        dest = OUT / f"criativo_{c['id']}_v2.png"
        html = build_html(foto, c["badge"], c["headline"], c["sub"], c["cta"])
        render_one(html, dest)
        print(f"[OK] {dest.name} (foto: {c['foto']})")


if __name__ == "__main__":
    ids = sys.argv[1:] if len(sys.argv) > 1 else None
    main(ids)
