#!/usr/bin/env bash
#
# OdooPilot one-line installer for macOS and Linux.
#
# Run with:
#   curl -fsSL https://equationx.ai/odoo-pilot/install.sh | bash
#
# What it does:
#   1. Checks for Node.js 20+
#   2. npm install -g pilot-for-odoo
#   3. Launches the interactive setup wizard (pilot-for-odoo-init)
#
# It never modifies your shell config, never asks for sudo unless your Node
# install requires it for global npm packages, and exits cleanly if Node is
# missing with a link to the official download.

set -eu

# --- Pretty output -----------------------------------------------------------

if [ -t 1 ] && [ -n "${TERM:-}" ] && command -v tput >/dev/null 2>&1; then
  BOLD=$(tput bold)
  DIM=$(tput dim)
  GREEN=$(tput setaf 2)
  YELLOW=$(tput setaf 3)
  RED=$(tput setaf 1)
  RESET=$(tput sgr0)
else
  BOLD="" DIM="" GREEN="" YELLOW="" RED="" RESET=""
fi

step()    { printf "\n${BOLD}%s${RESET}\n" "$*"; }
ok()      { printf "${GREEN}✓${RESET} %s\n" "$*"; }
warn()    { printf "${YELLOW}!${RESET} %s\n" "$*"; }
fail()    { printf "${RED}✗${RESET} %s\n" "$*" >&2; }

# --- Banner ------------------------------------------------------------------

printf "\n"
printf "${BOLD}OdooPilot installer${RESET}\n"
printf "${DIM}An AI agent for your Odoo CRM. Runs locally in Claude Desktop.${RESET}\n"

# --- Detect platform ---------------------------------------------------------

UNAME=$(uname -s)
case "$UNAME" in
  Darwin*) PLATFORM="macOS";;
  Linux*)  PLATFORM="Linux";;
  *)       PLATFORM="$UNAME";;
esac
ok "Platform: $PLATFORM"

# --- Check Node.js -----------------------------------------------------------

step "Checking Node.js..."

if ! command -v node >/dev/null 2>&1; then
  fail "Node.js is not installed."
  echo
  echo "OdooPilot needs Node.js 20 or newer."
  echo
  if [ "$PLATFORM" = "macOS" ]; then
    echo "  Easiest path: download the macOS installer from"
    echo "  ${BOLD}https://nodejs.org${RESET} (click the LTS button)"
    echo
    echo "  Or with Homebrew: ${BOLD}brew install node${RESET}"
  elif [ "$PLATFORM" = "Linux" ]; then
    echo "  Recommended: NodeSource installer"
    echo "  See ${BOLD}https://nodejs.org/en/download/package-manager${RESET}"
  else
    echo "  Download from ${BOLD}https://nodejs.org${RESET}"
  fi
  echo
  echo "After installing Node, re-run:"
  echo "  ${BOLD}curl -fsSL https://equationx.ai/odoo-pilot/install.sh | bash${RESET}"
  exit 1
fi

NODE_RAW=$(node --version)
NODE_MAJOR=$(printf "%s" "$NODE_RAW" | sed 's/v//' | cut -d. -f1)
if [ "$NODE_MAJOR" -lt 20 ]; then
  fail "Node.js $NODE_RAW is too old. OdooPilot requires 20 or newer."
  echo "  Update from https://nodejs.org"
  exit 1
fi
ok "Node.js $NODE_RAW detected"

# --- npm install -------------------------------------------------------------

step "Installing pilot-for-odoo from npm..."

if npm install -g pilot-for-odoo >/dev/null 2>&1; then
  ok "Installed"
else
  warn "Global npm install failed without sudo. Retrying with sudo..."
  echo "  (Some Node installations need sudo for global packages.)"
  if sudo npm install -g pilot-for-odoo; then
    ok "Installed (with sudo)"
  else
    fail "npm install failed."
    echo "  Try manually: ${BOLD}npm install -g pilot-for-odoo${RESET}"
    exit 1
  fi
fi

# --- Hand off to the interactive wizard --------------------------------------

step "Launching setup wizard..."
echo "${DIM}The wizard will ask for your Odoo URL, database, login, and API key."
echo "Have your MCP Connector wizard output from Odoo ready to paste.${RESET}"
echo

# `exec` so the wizard owns the TTY cleanly — no installer chrome after it.
exec pilot-for-odoo-init
