#!/usr/bin/env bash
# MFTPlus Agent Installer
# Usage: curl -fsSL https://releases.mftplus.co.za/latest/install.sh | sh

set -euo pipefail

RELEASES_URL="https://releases.mftplus.co.za/latest"

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

info()  { echo -e "${GREEN}[info]${NC} $*"; }
warn()  { echo -e "${YELLOW}[warn]${NC} $*"; }
error() { echo -e "${RED}[error]${NC} $*" >&2; exit 1; }

# Detect OS and architecture
detect_platform() {
  local os arch

  case "$(uname -s)" in
    Linux)  os="linux" ;;
    Darwin) os="macos" ;;
    MINGW*|MSYS*|CYGWIN*) os="windows" ;;
    *) error "Unsupported OS: $(uname -s)" ;;
  esac

  case "$(uname -m)" in
    x86_64|amd64)    arch="amd64" ;;
    aarch64|arm64)   arch="arm64" ;;
    *) error "Unsupported architecture: $(uname -m)" ;;
  esac

  echo "${os}_${arch}"
}

install_linux_deb() {
  local tmpdir
  tmpdir=$(mktemp -d)
  trap 'rm -rf "$tmpdir"' EXIT

  info "Downloading MFTPlus Agent (deb)..."
  curl -fsSL "${RELEASES_URL}/MFT.Agent_0.1.0_amd64.deb" -o "${tmpdir}/mftplus-agent.deb"

  info "Installing..."
  sudo dpkg -i "${tmpdir}/mftplus-agent.deb" 2>/dev/null || sudo apt-get install -f -y "${tmpdir}/mftplus-agent.deb"

  info "Installed successfully. Configure with: mftctl config set-server <server-url>"
}

install_linux_rpm() {
  local tmpdir
  tmpdir=$(mktemp -d)
  trap 'rm -rf "$tmpdir"' EXIT

  info "Downloading MFTPlus Agent (rpm)..."
  curl -fsSL "${RELEASES_URL}/MFT.Agent-0.1.0-1.x86_64.rpm" -o "${tmpdir}/mftplus-agent.rpm"

  info "Installing..."
  sudo rpm -i "${tmpdir}/mftplus-agent.rpm"

  info "Installed successfully. Configure with: mftctl config set-server <server-url>"
}

install_linux_appimage() {
  local bindir="${HOME}/.local/bin"
  mkdir -p "$bindir"

  info "Downloading MFTPlus Agent (AppImage)..."
  curl -fsSL "${RELEASES_URL}/MFT.Agent_0.1.0_amd64.AppImage" -o "${bindir}/mftplus-agent"
  chmod +x "${bindir}/mftplus-agent"

  info "Installed to ${bindir}/mftplus-agent"
  info "Ensure ${bindir} is in your PATH."
}

install_cli_linux() {
  local tmpdir
  tmpdir=$(mktemp -d)
  trap 'rm -rf "$tmpdir"' EXIT

  info "Downloading mftctl CLI..."
  curl -fsSL "${RELEASES_URL}/mft-agent-cli_0.1.0_linux_amd64.tar.gz" -o "${tmpdir}/mftctl.tar.gz"

  info "Extracting..."
  tar xzf "${tmpdir}/mftctl.tar.gz" -C "${tmpdir}"

  sudo mkdir -p /usr/local/bin
  sudo cp "${tmpdir}"/mft-agent-cli_*/mft-agent-cli /usr/local/bin/mftctl
  sudo chmod +x /usr/local/bin/mftctl

  info "mftctl installed to /usr/local/bin/mftctl"
}

install_macos() {
  warn "macOS binary not yet available on the releases server."
  warn "Download from: ${RELEASES_URL}/"
  error "macOS install not supported in this version of the installer."
}

install_windows() {
  warn "Windows binary not yet available on the releases server."
  warn "Download from: ${RELEASES_URL}/"
  error "Windows install not supported in this version of the installer."
}

main() {
  echo "MFTPlus Agent Installer"
  echo "======================="
  echo

  local platform
  platform=$(detect_platform)
  local os="${platform%%_*}"
  local arch="${platform##*_}"

  info "Detected: ${os} (${arch})"
  echo

  # Check for CLI-only install
  if [ "${1:-}" = "--cli" ] || [ "${1:-}" = "cli" ]; then
    case "$os" in
      linux)  install_cli_linux ;;
      *)      error "CLI install only supported on Linux currently" ;;
    esac
    return
  fi

  case "$os" in
    linux)
      # Prefer deb on Debian/Ubuntu, rpm on RHEL/CentOS, AppImage as fallback
      if command -v dpkg &>/dev/null; then
        install_linux_deb
      elif command -v rpm &>/dev/null; then
        install_linux_rpm
      else
        install_linux_appimage
      fi
      ;;
    macos)   install_macos ;;
    windows) install_windows ;;
  esac
}

main "$@"
