#!/usr/bin/env bash
# ============================================================================
# madarX OS installer (macOS / Linux)
#
#   curl -fsSL https://raw.githubusercontent.com/kareemodev777/madarx-os/main/scripts/install.sh | bash
#
# Installs the `madarx` command globally via npm, then (if running in an
# interactive terminal) launches the setup wizard. Data lives in ~/.madarx,
# so upgrades never touch your DB, workspace, or secrets.
# ============================================================================
set -euo pipefail

PKG="madarx"
MIN_NODE_MAJOR=22
MIN_NODE_MINOR=5

red()   { printf '\033[0;31m%s\033[0m\n' "$1"; }
green() { printf '\033[0;32m%s\033[0m\n' "$1"; }
blue()  { printf '\033[0;34m%s\033[0m\n' "$1"; }

blue "madarX OS installer"

# 1. Node present?
if ! command -v node >/dev/null 2>&1; then
  red "Node.js is not installed."
  echo "madarX OS needs Node.js >= ${MIN_NODE_MAJOR}.${MIN_NODE_MINOR} (for the built-in node:sqlite module)."
  echo "Install it, then re-run this script. Easiest cross-platform option — fnm:"
  echo "  curl -fsSL https://fnm.vercel.app/install | bash && fnm install 22"
  echo "Or grab an installer from https://nodejs.org/ (choose 22 LTS or newer)."
  exit 1
fi

# 2. Node new enough?
NODE_VER="$(node -v | sed 's/^v//')"
NODE_MAJOR="$(printf '%s' "$NODE_VER" | cut -d. -f1)"
NODE_MINOR="$(printf '%s' "$NODE_VER" | cut -d. -f2)"
too_old=0
if [ "$NODE_MAJOR" -lt "$MIN_NODE_MAJOR" ]; then
  too_old=1
elif [ "$NODE_MAJOR" -eq "$MIN_NODE_MAJOR" ] && [ "$NODE_MINOR" -lt "$MIN_NODE_MINOR" ]; then
  too_old=1
fi
if [ "$too_old" -eq 1 ]; then
  red "Node.js ${NODE_VER} is too old — madarX OS needs >= ${MIN_NODE_MAJOR}.${MIN_NODE_MINOR}."
  echo "Upgrade Node (e.g. 'fnm install 22' or 'nvm install 22'), then re-run this script."
  exit 1
fi
green "Node.js ${NODE_VER} OK"

# 3. npm present?
if ! command -v npm >/dev/null 2>&1; then
  red "npm is not installed (it ships with Node.js). Reinstall Node.js and try again."
  exit 1
fi

# 4. Install globally. If the global prefix isn't writable, fall back to a
#    user-owned prefix so we never need sudo.
blue "Installing ${PKG} globally…"
if npm install -g "${PKG}@latest" --no-audit --no-fund; then
  green "Installed."
else
  red "Global install failed — your npm prefix may not be writable without sudo."
  echo "Set a user-owned prefix and retry (no sudo needed):"
  echo "  mkdir -p \"\$HOME/.npm-global\""
  echo "  npm config set prefix \"\$HOME/.npm-global\""
  echo "  export PATH=\"\$HOME/.npm-global/bin:\$PATH\"   # add to ~/.zshrc or ~/.bashrc"
  echo "  npm install -g ${PKG}@latest"
  exit 1
fi

# 5. PATH sanity check.
if ! command -v madarx >/dev/null 2>&1; then
  red "Installed, but 'madarx' isn't on your PATH yet."
  echo "Add your npm global bin to PATH, then restart your shell:"
  echo "  export PATH=\"\$(npm prefix -g)/bin:\$PATH\""
  exit 0
fi

# 6. Setup wizard, only if we have a real terminal.
if [ -t 0 ] && [ -t 1 ]; then
  blue "Launching setup…"
  madarx setup || true
else
  green "Installed. Next steps:"
  echo "  madarx setup     # configure the master key, folder permissions, keys"
  echo "  madarx           # start everything (web + cron + heartbeat + WhatsApp)"
fi

green "Done. Start madarX OS anytime with:  madarx"
