#!/usr/bin/env bash
set -euo pipefail

repo_root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$repo_root"

node_version="24.13.0"
started_at=$SECONDS

echo "[setup] Preparing SpellRay in $repo_root"

if [[ "$(node --version 2>/dev/null || true)" != "v${node_version}" ]]; then
  echo "[setup] Installing Node.js ${node_version}"
  npx --yes n@10.2.0 "$node_version"
  hash -r
else
  echo "[setup] Node.js ${node_version} is already installed"
fi

echo "[setup] Using Node.js $(node --version) and npm $(npm --version)"
echo "[setup] Installing dependencies from package-lock.json"
npm ci --no-audit --no-fund

echo "[setup] Complete in $((SECONDS - started_at))s"
