#!/bin/bash
#
# Install script for wa - CaseMgr worktree manager
#
# Usage:
#   curl -fsSL https://casemgr.systems/cli/install.sh | bash
#   curl -fsSL https://casemgr.systems/cli/install.sh | INSTALL_DIR=/usr/local/bin bash
#

set -euo pipefail

BASE_URL="https://casemgr.systems/cli"
INSTALL_DIR="${INSTALL_DIR:-$HOME/.local/bin}"

# Detect OS
detect_os() {
  case "$(uname -s)" in
    Linux*)  echo "linux" ;;
    Darwin*) echo "macos" ;;
    *)       echo "unsupported" ;;
  esac
}

OS=$(detect_os)

if [ "$OS" = "unsupported" ]; then
  echo "Error: Unsupported operating system: $(uname -s)"
  echo "wa currently supports Linux and macOS."
  exit 1
fi

# Check for Erlang/OTP runtime (required for escripts)
if ! command -v erl >/dev/null 2>&1; then
  echo "Error: Erlang/OTP is required but not installed."
  echo ""
  if [ "$OS" = "macos" ]; then
    echo "Install with Homebrew:"
    echo "  brew install erlang"
  else
    echo "Install on Ubuntu/Debian:"
    echo "  sudo apt-get install erlang-base"
    echo ""
    echo "Install on Fedora/RHEL:"
    echo "  sudo dnf install erlang"
  fi
  exit 1
fi

echo "Installing wa CLI..."
echo ""

# Create install directory
mkdir -p "$INSTALL_DIR"

# Download the wa binary
echo "  Downloading wa..."
if command -v curl >/dev/null 2>&1; then
  curl -fsSL "$BASE_URL/wa" -o "$INSTALL_DIR/wa"
elif command -v wget >/dev/null 2>&1; then
  wget -q "$BASE_URL/wa" -O "$INSTALL_DIR/wa"
else
  echo "Error: curl or wget is required."
  exit 1
fi

chmod +x "$INSTALL_DIR/wa"
echo "  Installed wa to $INSTALL_DIR/wa"

# Download shell functions
echo "  Downloading shell functions..."
SHELL_FUNCS_DIR="$HOME/.config/wa"
mkdir -p "$SHELL_FUNCS_DIR"

download() {
  local file="$1"
  if command -v curl >/dev/null 2>&1; then
    curl -fsSL "$BASE_URL/$file" -o "$SHELL_FUNCS_DIR/$file"
  else
    wget -q "$BASE_URL/$file" -O "$SHELL_FUNCS_DIR/$file"
  fi
}

download "workarea-functions.sh"
download "git-helpers.sh"
download "project-functions.sh"

echo "  Installed shell functions to $SHELL_FUNCS_DIR/"

# Check if INSTALL_DIR is in PATH
if ! echo "$PATH" | tr ':' '\n' | grep -qx "$INSTALL_DIR"; then
  echo ""
  echo "  NOTE: $INSTALL_DIR is not in your PATH."
  echo "  Add this to your shell profile (~/.bashrc, ~/.zshrc, etc.):"
  echo ""
  echo "    export PATH=\"$INSTALL_DIR:\$PATH\""
fi

# Detect shell profile
SHELL_PROFILE=""
if [ -f "$HOME/.bashrc" ]; then
  SHELL_PROFILE="$HOME/.bashrc"
elif [ -f "$HOME/.zshrc" ]; then
  SHELL_PROFILE="$HOME/.zshrc"
elif [ -f "$HOME/.bash_profile" ]; then
  SHELL_PROFILE="$HOME/.bash_profile"
fi

# Offer to add shell integration
SHELL_LINES="source $SHELL_FUNCS_DIR/workarea-functions.sh
source $SHELL_FUNCS_DIR/git-helpers.sh
source $SHELL_FUNCS_DIR/project-functions.sh"

if [ -n "$SHELL_PROFILE" ] && ! grep -q "workarea-functions.sh" "$SHELL_PROFILE" 2>/dev/null; then
  echo ""
  echo "  Add shell helpers to $SHELL_PROFILE? (cdwa, home, aliases, git shortcuts)"
  printf "  [Y/n] "
  read -r REPLY
  if [ -z "$REPLY" ] || [ "$REPLY" = "y" ] || [ "$REPLY" = "Y" ]; then
    echo "" >> "$SHELL_PROFILE"
    echo "# wa - CaseMgr Worktree Manager" >> "$SHELL_PROFILE"
    echo "$SHELL_LINES" >> "$SHELL_PROFILE"
    echo "  Added to $SHELL_PROFILE"
  fi
else
  echo ""
  echo "  To enable shell helpers (cdwa, home, aliases, git shortcuts),"
  echo "  add these to your shell profile:"
  echo ""
  echo "    source $SHELL_FUNCS_DIR/workarea-functions.sh"
  echo "    source $SHELL_FUNCS_DIR/git-helpers.sh"
  echo "    source $SHELL_FUNCS_DIR/project-functions.sh"
fi

# Verify installation
echo ""
if "$INSTALL_DIR/wa" --version >/dev/null 2>&1; then
  VERSION=$("$INSTALL_DIR/wa" --version)
  echo "  $VERSION installed successfully!"
else
  echo "  wa installed (could not verify version)."
fi

echo ""
echo "  Next steps:"
echo "    1. wa login              # Authenticate with CaseMgr"
echo "    2. wa help               # See available commands"
echo "    3. wa ls wt              # List worktrees"
echo ""
