#!/usr/bin/env bash
# ==============================================================================
#  🚀 NginxForge One-Line Quick Installer
#  Usage: curl -fsSL https://get.nginxforge.dev | bash
#         or ./install.sh [-y] [--dry-run]
# ==============================================================================

set -e

COLOR_RESET="\033[0m"
COLOR_GREEN="\033[32m"
COLOR_YELLOW="\033[33m"
COLOR_CYAN="\033[36m"
COLOR_RED="\033[31m"
COLOR_BOLD="\033[1m"

INSTALL_DIR="/var/lib/nginxforge/app"
SERVICE_NAME="nginxforge.service"
PORT="3222"

echo -e "\n================================================================="
echo -e " ${COLOR_BOLD}🚀 NGINXFORGE PRODUCTION VPS QUICK INSTALLER${COLOR_RESET} "
echo -e "=================================================================\n"

# 1. Root / Sudo Check
if [ "$EUID" -ne 0 ]; then
  if command -v sudo >/dev/null 2>&1; then
    SUDO="sudo"
  else
    echo -e "${COLOR_RED}✖ Error: This installer must be run as root or with sudo privileges.${COLOR_RESET}"
    exit 1
  fi
else
  SUDO=""
fi

# 2. Detect OS & Package Manager
echo -e "${COLOR_CYAN}ℹ Detecting Linux distribution and package manager...${COLOR_RESET}"
if [ -f /etc/os-release ]; then
  . /etc/os-release
  OS=$ID
  VERSION=$VERSION_ID
else
  OS=$(uname -s | tr '[:upper:]' '[:lower:]')
fi

echo -e "${COLOR_GREEN}✔ Detected OS:${COLOR_RESET} ${COLOR_BOLD}$PRETTY_NAME ($OS $VERSION)${COLOR_RESET}\n"

# 3. Ensure Node.js & Git Are Installed
if ! command -v git >/dev/null 2>&1; then
  echo -e "${COLOR_YELLOW}⚠ Git is not installed. Installing Git...${COLOR_RESET}"
  if [ "$OS" = "ubuntu" ] || [ "$OS" = "debian" ]; then
    $SUDO apt-get update -y && $SUDO apt-get install -y git curl
  elif [ "$OS" = "centos" ] || [ "$OS" = "rhel" ] || [ "$OS" = "fedora" ] || [ "$OS" = "rocky" ] || [ "$OS" = "almalinux" ]; then
    $SUDO dnf install -y git curl || $SUDO yum install -y git curl
  elif [ "$OS" = "arch" ]; then
    $SUDO pacman -Sy --noconfirm git curl
  elif [ "$OS" = "alpine" ]; then
    $SUDO apk add git curl bash
  fi
fi

if ! command -v node >/dev/null 2>&1; then
  echo -e "${COLOR_YELLOW}⚠ Node.js is not installed. Installing Node.js LTS (v22)...${COLOR_RESET}"
  if [ "$OS" = "ubuntu" ] || [ "$OS" = "debian" ]; then
    curl -fsSL https://deb.nodesource.com/setup_22.x | $SUDO bash -
    $SUDO apt-get install -y nodejs
  elif [ "$OS" = "centos" ] || [ "$OS" = "rhel" ] || [ "$OS" = "fedora" ] || [ "$OS" = "rocky" ] || [ "$OS" = "almalinux" ]; then
    curl -fsSL https://rpm.nodesource.com/setup_22.x | $SUDO bash -
    $SUDO dnf install -y nodejs || $SUDO yum install -y nodejs
  elif [ "$OS" = "arch" ]; then
    $SUDO pacman -Sy --noconfirm nodejs npm
  elif [ "$OS" = "alpine" ]; then
    $SUDO apk add nodejs npm
  fi
fi

# Ensure pnpm is available
if ! command -v pnpm >/dev/null 2>&1; then
  echo -e "${COLOR_CYAN}ℹ Installing pnpm...${COLOR_RESET}"
  $SUDO npm install -g pnpm >/dev/null 2>&1 || true
fi

# 4. Run Interactive System Dependency & Tools Installer
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ -f "$SCRIPT_DIR/scripts/system-installer.mjs" ]; then
  node "$SCRIPT_DIR/scripts/system-installer.mjs" "$@"
elif [ -f "$INSTALL_DIR/scripts/system-installer.mjs" ]; then
  node "$INSTALL_DIR/scripts/system-installer.mjs" "$@"
else
  # Running from curl pipe
  TMP_INSTALLER="/tmp/nfx_system_installer_$$.mjs"
  curl -fsSL "https://raw.githubusercontent.com/nginxforge/nginxforge/main/scripts/system-installer.mjs" -o "$TMP_INSTALLER" 2>/dev/null || true
  if [ -f "$TMP_INSTALLER" ]; then
    node "$TMP_INSTALLER" "$@"
    rm -f "$TMP_INSTALLER"
  fi
fi

echo -e "\n================================================================="
echo -e " ${COLOR_GREEN}✔ NGINXFORGE INSTALLATION & ENVIRONMENT INITIALIZED!${COLOR_RESET} "
echo -e "=================================================================\n"
echo -e "To launch or manage the NginxForge control plane:"
echo -e "  • Start Dashboard:  ${COLOR_CYAN}pnpm start${COLOR_RESET} or ${COLOR_CYAN}systemctl start nginxforge${COLOR_RESET}"
echo -e "  • Panel URL:        ${COLOR_BOLD}http://<YOUR-SERVER-IP>:3222${COLOR_RESET}"
echo -e "  • Re-run Setup:     ${COLOR_CYAN}npx nginxforge setup${COLOR_RESET}\n"
