"""
Correcao scene 2 do Reel A: reposicionar foto + texto para nao sobrepor rosto
"""
from playwright.sync_api import sync_playwright
import os, base64

W, H = 1080, 1920
OUT = "/opt/mia/workspace/clientes/borrello/criativos_meta_aniversario/pipeline_v4"
FRAMES = f"{OUT}/frames_a"

PENDULO_IMG = "/opt/mia/workspace/clientes/francisco_borrello/_recortes/_MG_9366_cutout.png"
with open(PENDULO_IMG, "rb") as f:
    pendulo_b64 = base64.b64encode(f.read()).decode()

HEAD = """
<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=Cormorant+Garamond:ital,wght@0,300;0,400;0,500;0,600;0,700;1,400&family=Inter:wght@300;400;500;600&display=swap" rel="stylesheet">
<style>
  * { margin:0; padding:0; box-sizing:border-box; }
  body { margin:0; overflow:hidden; width:1080px; height:1920px;
    font-family:'Inter',sans-serif; color:#f4e9d0;
    background: #050505; position:relative; }
  .bg {
    position:absolute; inset:0;
    background:
      radial-gradient(ellipse 60% 40% at 25% 30%, rgba(212, 155, 65, 0.35), transparent 70%),
      radial-gradient(ellipse 80% 60% at 75% 75%, rgba(30, 20, 50, 0.6), transparent 70%),
      linear-gradient(180deg, #08060a 0%, #100a08 40%, #05030a 100%);
  }
  .grain {
    position:absolute; inset:0;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='300'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 1  0 0 0 0 0.85  0 0 0 0 0.5  0 0 0 0.08 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
    mix-blend-mode: overlay; opacity: .4;
  }
  .vignette {
    position:absolute; inset:0;
    background: radial-gradient(ellipse 90% 90% at 50% 50%, transparent 40%, rgba(0,0,0,.85) 100%);
  }
  /* Foto grande, ocupa 2/3 superior */
  .photo_frame {
    position:absolute;
    top: 0; left: 50%;
    transform: translateX(-50%);
    height: 1350px;
    width: auto;
    z-index: 2;
  }
  .photo_frame img {
    height: 100%;
    width: auto;
    display: block;
    filter: brightness(1.05) contrast(1.1) saturate(0.95);
  }
  /* Faixa escura embaixo pro texto */
  .text_panel {
    position: absolute;
    bottom: 0; left: 0; right: 0;
    padding: 80px 90px 130px;
    text-align: center;
    z-index: 5;
    background: linear-gradient(180deg, transparent 0%, rgba(5,5,10,0.85) 25%, rgba(5,5,10,0.98) 60%);
    height: 700px;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
  }
  .kicker {
    font-family: 'Inter', sans-serif;
    font-weight: 500;
    font-size: 38px;
    letter-spacing: 7px;
    text-transform: uppercase;
    color: #d4a04c;
    margin-bottom: 30px;
    opacity: .9;
  }
  h1 {
    font-family: 'Cormorant Garamond', serif;
    font-weight: 400;
    font-size: 150px;
    line-height: 1;
    color: #f4e9d0;
    letter-spacing: -2px;
  }
  h1 em {
    font-style: italic;
    color: #e8c479;
    font-weight: 500;
  }
</style>
"""

body = f"""
<div class="bg"></div>
<div class="photo_frame">
  <img src="data:image/png;base64,{pendulo_b64}" />
</div>
<div class="grain"></div>
<div class="vignette"></div>
<div class="text_panel">
  <div class="kicker">Chama-se</div>
  <h1><em>Radiestesia.</em></h1>
</div>
"""

html = f"<!DOCTYPE html><html><head>{HEAD}</head><body>{body}</body></html>"
with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page(viewport={"width":W,"height":H})
    page.set_content(html, wait_until="networkidle")
    page.wait_for_timeout(400)
    page.screenshot(path=f"{FRAMES}/scene_02.png", clip={"x":0,"y":0,"width":W,"height":H})
    browser.close()
print("scene_02 refeita.")
