#!/usr/bin/env bash
# scripts/render_slides.sh — render the Nova presentation deck end-to-end:
#   1. Mermaid .mmd → .png (S&P-themed via sp-theme.json)
#   2. Marp .md → .html + .pptx (S&P-themed via inline style: block in frontmatter)
#   3. Inline images → base64-embed all images in the HTML (self-contained).
#   4. Stage all rendered artifacts to git.
#
# Usage:
#   bash scripts/render_slides.sh [deck-name]
#   bash scripts/render_slides.sh nova-autonomous-cloud-delivery
#
# If no deck name is given, defaults to nova-autonomous-cloud-delivery.
set -euo pipefail

DECK="${1:-nova-autonomous-cloud-delivery}"
cd "$(git rev-parse --show-toplevel)"

MMD_DIR="docs/presentations/assets/mmd"
PNG_DIR="docs/presentations/assets/png"
THEME_JSON="$MMD_DIR/sp-theme.json"
PUPPETEER_CFG="docs/presentations/assets/puppeteer-config.json"
SRC="docs/presentations/${DECK}-marp.md"
HTML="docs/presentations/${DECK}.html"
PPTX="docs/presentations/${DECK}.pptx"

[ -f "$SRC" ] || { echo "ERROR: source deck $SRC not found" >&2; exit 1; }

# --- Chrome / Chromium discovery ---
CHROME=""
for c in \
  /root/.cache/ms-playwright/chromium-1217/chrome-linux64/chrome \
  /usr/bin/chromium \
  /usr/bin/chromium-browser \
  /usr/bin/google-chrome; do
  if [ -x "$c" ]; then CHROME="$c"; break; fi
done
if [ -z "$CHROME" ]; then
  echo "WARNING: no Chrome/Chromium found — skipping render" >&2
  exit 0
fi
export CHROME_PATH="$CHROME"

# --- Step 1: render mermaid diagrams (S&P-themed, 2x scale, transparent bg) ---
# REQ-258: -s 2 (2x scale) + -b transparent (transparent background) per README spec.
echo "=== Step 1: Rendering mermaid diagrams (S&P theme: $THEME_JSON, 2x, transparent) ==="
if [ -d "$MMD_DIR" ]; then
  for mmd in "$MMD_DIR"/*.mmd; do
    name="$(basename "$mmd" .mmd)"
    png="$PNG_DIR/$name.png"
    echo "  $name.mmd → $name.png"
    npx --yes @mermaid-js/mermaid-cli@11.16.0 \
      --configFile "$THEME_JSON" \
      --puppeteerConfigFile "$PUPPETEER_CFG" \
      -s 2 -b transparent \
      --input "$mmd" \
      --output "$png" 2>&1 | grep -v "^$" || true
  done
  echo "  Rendered $(ls "$PNG_DIR"/*.png 2>/dev/null | wc -l) PNGs."
else
  echo "  No .mmd directory — skipping mermaid render."
fi
echo ""

# --- Step 2: render Marp deck (S&P-themed) ---
# REQ-257: pinned marp-cli version (v4.5.0) to prevent boilerplate-CSS drift.
echo "=== Step 2: Rendering Marp deck → HTML + PPTX ==="
echo "  Theme: default (inline style)"
echo "  HTML → $HTML"
npx --yes @marp-team/marp-cli@4.5.0 --allow-local-files "$SRC" -o "$HTML" 2>&1 | tail -3

echo "  PPTX → $PPTX"
npx --yes @marp-team/marp-cli@4.5.0 --allow-local-files "$SRC" -o "$PPTX" 2>&1 | tail -3
echo ""

# --- Step 3: inline images into HTML (base64-embed for redistribution) ---
# REQ-268: makes the HTML self-contained (no assets/ folder needed).
echo "=== Step 3: Inlining images into HTML ==="
python3 scripts/inline_images.py "$HTML" 2>&1
echo ""

# --- Step 4: render python-pptx (structured, editable, S&P-themed) ---
# REQ-269: python-pptx produces a structured, editable PPTX (native text boxes,
# tables, images) alongside the MARP-rendered PPTX.
echo "=== Step 4: Rendering python-pptx deck ==="
PYTHON_PPTX="docs/presentations/${DECK}-python.pptx"
python3 scripts/render_pptx.py "$DECK" 2>&1
echo "  Python PPTX → $PYTHON_PPTX"
echo ""

# --- Step 5: stage ---
echo "=== Step 5: Staging rendered artifacts ==="
git add "$PNG_DIR"/*.png "$HTML" "$PPTX" "$PYTHON_PPTX" 2>/dev/null || true
echo "=== Done: staged $(ls "$PNG_DIR"/*.png 2>/dev/null | wc -l) PNGs + $HTML + $PPTX + $PYTHON_PPTX ==="
