#!/bin/bash
# Renderiza os HTMLs de uma pasta em PNG usando Chrome headless.
# Uso: ./render.sh <pasta-com-htmls> [largura] [altura]
#   Carrossel/arte fixa: ./render.sh ./saida 1080 1350
#   Capa de reels:       ./render.sh ./saida 1080 1920
set -euo pipefail

DIR="${1:?Uso: render.sh <pasta> [w] [h]}"
W="${2:-1080}"
H="${3:-1350}"

CHROME="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
[ -x "$CHROME" ] || CHROME="/Applications/Chromium.app/Contents/MacOS/Chromium"
[ -x "$CHROME" ] || { echo "Chrome não encontrado"; exit 1; }

shopt -s nullglob
for f in "$DIR"/*.html; do
  out="${f%.html}.png"
  # virtual-time-budget dá tempo das fontes do Google (DM Sans/Playfair) carregarem antes do screenshot
  "$CHROME" --headless=new --disable-gpu --hide-scrollbars \
    --force-device-scale-factor=1 \
    --virtual-time-budget=6000 \
    --window-size="${W},${H}" \
    --screenshot="$out" \
    "file://$(cd "$(dirname "$f")" && pwd)/$(basename "$f")" 2>/dev/null
  echo "✅ $(basename "$out")"
done
echo "Pronto: $(ls "$DIR"/*.png 2>/dev/null | wc -l | tr -d ' ') PNGs em $DIR"
