#!/usr/bin/env bash
# =============================================================================
# install.sh — wcag-audit setup
#
# Installs the npm packages and the Chromium browser binary this service needs,
# then restarts the PM2 process if one is already running.
#
# Chromium is the only browser this service drives — it's used both to scan
# pages with axe-core and, a second time, to render the PDF export via
# page.pdf() — so Firefox/WebKit are deliberately not installed.
#
# Usage:
#   chmod +x install.sh
#   ./install.sh
# =============================================================================
set -euo pipefail

BOLD='\033[1m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
RESET='\033[0m'

step() { echo -e "\n${CYAN}${BOLD}[${1}/${TOTAL}]${RESET} ${BOLD}${2}${RESET}"; }
ok()   { echo -e "${GREEN}✓ ${1}${RESET}"; }
warn() { echo -e "${YELLOW}⚠ ${1}${RESET}"; }

TOTAL=3

echo -e "\n${BOLD}wcag-audit — setup${RESET}"
echo "──────────────────────────────────────"

# ── 1. Node.js / npm present? ─────────────────────────────────────────────────
step 1 "Checking Node.js / npm"
if ! command -v node >/dev/null 2>&1 || ! command -v npm >/dev/null 2>&1; then
  echo "ERROR: Node.js/npm not found. Install Node.js >= 18 (e.g. via nvm), then re-run this script."
  exit 1
fi
ok "Node.js $(node --version) / npm $(npm --version)"

# ── 2. npm packages ───────────────────────────────────────────────────────────
step 2 "Installing npm packages (incl. axe-core)"
npm install --prefer-offline 2>&1 | tail -3
node -e "require.resolve('axe-core'); console.log('axe-core ' + require('axe-core/package.json').version)" \
  || { echo "ERROR: axe-core did not install correctly."; exit 1; }
ok "npm packages installed"

# ── 3. Chromium binary ────────────────────────────────────────────────────────
step 3 "Installing Playwright's Chromium binary"
if [ "$(id -u)" -eq 0 ]; then
  npx playwright install --with-deps chromium
else
  sudo npx playwright install --with-deps chromium 2>/dev/null || npx playwright install chromium
fi
ok "Chromium installed"

# ── Restart the running service, if any ───────────────────────────────────────
if command -v pm2 >/dev/null 2>&1 && pm2 describe wcag-audit >/dev/null 2>&1; then
  echo ""
  warn "Restarting the running wcag-audit PM2 process…"
  pm2 restart wcag-audit
  ok "wcag-audit restarted"
fi

# ── Summary ───────────────────────────────────────────────────────────────────
PORT=${PORT:-4200}
echo ""
echo "──────────────────────────────────────"
echo -e "${GREEN}${BOLD}✓ wcag-audit is ready${RESET}"
echo ""
echo -e "  UI:   ${BOLD}http://localhost:${PORT}/${RESET}"
echo "  API:  POST /a11y-audit   body: urls=<newline/comma-separated, up to 10>"
echo "                            or:  mode=sitemap&sitemapUrl=…&maxPages=20"
echo "        GET  /stream/:runId   — SSE progress (progress | done | error)"
echo "        GET  /reports/:runId/ — a11y-report.html + .csv + .xlsx + .pdf"
echo ""
echo "  Start it with:"
echo "    npm start            (foreground)"
echo "    npm run start:pm2    (background via PM2)"
echo "──────────────────────────────────────"
