#!/usr/bin/env bash
# serve-presentation: Launch localhost presentation server with live reload
# Watches room/ directory, regenerates 6-view presentation on changes,
# SSE broadcasts reload to connected browsers.
# Starts Express server, opens browser (WSL-aware), stays alive until Ctrl+C

set -euo pipefail

ROOM_DIR="${ROOM_DIR:-./room}"
PREFERRED_PORT="${1:-8422}"
MAX_PORT=8430

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# -- Step 1: Find available port --
find_port() {
  local port="$1"
  local max="$2"
  while [ "$port" -le "$max" ]; do
    if python3 -c "import socket; s=socket.socket(); s.bind(('',${port})); s.close()" 2>/dev/null; then
      echo "$port"
      return 0
    fi
    port=$((port + 1))
  done
  return 1
}

PORT=$(find_port "$PREFERRED_PORT" "$MAX_PORT")
if [ -z "$PORT" ]; then
  echo "ERROR: No available port in range ${PREFERRED_PORT}-${MAX_PORT}"
  exit 1
fi

# -- Step 2: Verify presentation directory exists --
PRES_DIR="${ROOM_DIR}/exports/presentation"
if [ ! -d "$PRES_DIR" ]; then
  echo "No presentation found at ${PRES_DIR}"
  echo "Generating initial presentation views..."
  node "${SCRIPT_DIR}/generate-presentation.cjs" "$ROOM_DIR" --output "$PRES_DIR" || {
    echo "ERROR: Failed to generate presentation. Run generate-presentation.cjs first."
    exit 1
  }
fi

# -- Step 3: Start presentation server in background --
SERVER_PID=""
cleanup() {
  if [ -n "$SERVER_PID" ] && kill -0 "$SERVER_PID" 2>/dev/null; then
    kill "$SERVER_PID" 2>/dev/null || true
    wait "$SERVER_PID" 2>/dev/null || true
  fi
  echo ""
  echo "Presentation server stopped."
}
trap cleanup EXIT INT TERM

node "${SCRIPT_DIR}/../lib/presentation/presentation-server.cjs" "$ROOM_DIR" "$PORT" &
SERVER_PID=$!

# Brief pause to let server start
sleep 0.5

# Verify server started
if ! kill -0 "$SERVER_PID" 2>/dev/null; then
  echo "ERROR: Failed to start presentation server on port ${PORT}"
  exit 1
fi

# -- Step 4: Open browser (cross-platform) --
URL="http://localhost:${PORT}/"

open_browser() {
  local url="$1"
  if grep -qi microsoft /proc/version 2>/dev/null; then
    # WSL: use Windows browser
    cmd.exe /c start "" "$url" 2>/dev/null || true
  elif [ "$(uname)" = "Darwin" ]; then
    open "$url" 2>/dev/null || true
  elif command -v xdg-open >/dev/null 2>&1; then
    xdg-open "$url" >/dev/null 2>&1 &
  else
    echo "Open ${url} in your browser"
  fi
}

open_browser "$URL"

# -- Step 5: Print status --
echo ""
echo "Presentation running at ${URL} (PID: ${SERVER_PID})"
echo "Watching ${ROOM_DIR} for changes -- auto-regenerates + reloads browser"
echo "Press Ctrl+C to stop"
echo ""

# -- Step 6: Wait for server process --
wait "$SERVER_PID"
