#!/usr/bin/env bash
# SpriteCook MCP Setup - macOS / Linux
# Usage: bash -c "$(curl -fsSL https://spritecook.ai/install.sh)"
set -e

echo ""
echo "  SpriteCook MCP Installer"
echo ""

install_node_macos() {
  # Try Homebrew first
  if command -v brew &> /dev/null; then
    echo "  [info] Installing Node.js via Homebrew..."
    brew install node@22 2>/dev/null
    brew link node@22 --overwrite --force 2>/dev/null || true
    if command -v node &> /dev/null; then
      echo "  [ok] Node.js installed via Homebrew"
      return 0
    fi
  fi

  # Install Homebrew first if missing, then install Node
  echo "  [info] Homebrew not found. Installing Homebrew first..."
  /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

  # Add Homebrew to PATH for this session
  if [ -f "/opt/homebrew/bin/brew" ]; then
    eval "$(/opt/homebrew/bin/brew shellenv)"
  elif [ -f "/usr/local/bin/brew" ]; then
    eval "$(/usr/local/bin/brew shellenv)"
  fi

  if command -v brew &> /dev/null; then
    brew install node@22 2>/dev/null
    brew link node@22 --overwrite --force 2>/dev/null || true
    if command -v node &> /dev/null; then
      echo "  [ok] Node.js installed via Homebrew"
      return 0
    fi
  fi

  return 1
}

install_node_linux() {
  echo "  [info] Installing Node.js v22 via NodeSource..."

  # Determine if we need sudo
  SUDO=""
  if [ "$(id -u)" -ne 0 ]; then
    if command -v sudo &> /dev/null; then
      SUDO="sudo"
    else
      echo "  [error] Root privileges required. Run as root or install sudo."
      return 1
    fi
  fi

  if command -v apt-get &> /dev/null; then
    curl -fsSL https://deb.nodesource.com/setup_22.x | $SUDO bash -
    $SUDO apt-get install -y nodejs
  elif command -v dnf &> /dev/null; then
    curl -fsSL https://rpm.nodesource.com/setup_22.x | $SUDO bash -
    $SUDO dnf install -y nodejs
  elif command -v yum &> /dev/null; then
    curl -fsSL https://rpm.nodesource.com/setup_22.x | $SUDO bash -
    $SUDO yum install -y nodejs
  elif command -v apk &> /dev/null; then
    $SUDO apk add --no-cache nodejs npm
  else
    echo "  [error] Could not detect package manager (apt, dnf, yum, apk)"
    return 1
  fi

  if command -v node &> /dev/null; then
    echo "  [ok] Node.js installed"
    return 0
  fi

  return 1
}

# Check for Node.js - install if missing
if ! command -v node &> /dev/null; then
  echo "  [info] Node.js not found. Installing..."
  echo ""

  OS="unknown"
  case "$OSTYPE" in
    darwin*) OS="macos" ;;
    linux*)  OS="linux" ;;
  esac

  # Also detect WSL as Linux
  if [ -n "${WSL_DISTRO_NAME:-}" ]; then
    OS="linux"
  fi

  installed=false
  if [ "$OS" = "macos" ]; then
    install_node_macos && installed=true
  elif [ "$OS" = "linux" ]; then
    install_node_linux && installed=true
  fi

  if [ "$installed" = false ]; then
    echo "  [error] Could not install Node.js automatically."
    echo ""
    echo "  Please install Node.js v18+ manually from https://nodejs.org"
    echo "  Then re-run this script."
    exit 1
  fi
  echo ""
fi

# Check Node version >= 18
NODE_MAJOR=$(node -e "console.log(process.versions.node.split('.')[0])")
if [ "$NODE_MAJOR" -lt 18 ]; then
  echo "  [error] Node.js v18+ required (found v$(node --version))"
  echo ""
  echo "  Update from https://nodejs.org"
  exit 1
fi

# Check for npx
if ! command -v npx &> /dev/null; then
  echo "  [error] npx not found. It should come with Node.js."
  echo ""
  echo "  Try reinstalling Node.js from https://nodejs.org"
  exit 1
fi

# Run the setup
if [ -r /dev/tty ]; then
  exec npx spritecook-mcp@latest setup </dev/tty
fi

exec npx spritecook-mcp@latest setup
