#!/usr/bin/env bash
set -euo pipefail

# LunarCrush CLI installer
# curl -fsSL https://lunarcrush.ai/install.sh | bash

DOWNLOAD_BASE="http://lunarcrush.ai/cli"

# Colors (only in TTY)
if [[ -t 2 ]]; then
  R='\033[0;31m' G='\033[0;32m' B='\033[1m' D='\033[0;2m' N='\033[0m'
else
  R='' G='' B='' D='' N=''
fi

error() { echo -e "${R}error:${N} $*" >&2; exit 1; }
info() { echo -e "$*" >&2; }

# Requirements
command -v curl >/dev/null 2>&1 || error "curl is required"

# Detect platform
os=$(uname -s)
arch=$(uname -m)

case "$os" in
  Linux)
    case "$arch" in
      x86_64|amd64) target="lunarcrush-linux-x64" ;;
      *) error "Unsupported Linux architecture: $arch" ;;
    esac
    ;;
  Darwin)
    case "$arch" in
      arm64) target="lunarcrush-darwin-arm64" ;;
      x86_64)
        # Rosetta 2 detection — use native arm64 binary if on Apple Silicon
        if [[ $(sysctl -n sysctl.proc_translated 2>/dev/null) == "1" ]]; then
          target="lunarcrush-darwin-arm64"
        else
          target="lunarcrush-darwin-x64"
        fi
        ;;
      *) error "Unsupported macOS architecture: $arch" ;;
    esac
    ;;
  MINGW*|MSYS*|CYGWIN*)
    error "Windows is not supported by this installer. Download manually from ${DOWNLOAD_BASE}/lunarcrush-windows-x64.exe"
    ;;
  *)
    error "Unsupported OS: $os"
    ;;
esac

# Install location — prefer ~/.local/bin if it's on PATH, otherwise fall back to ~/.lunarcrush/bin
if [[ ":${PATH}:" == *":$HOME/.local/bin:"* ]]; then
  bin_dir="$HOME/.local/bin"
elif [[ -d "$HOME/.local/bin" ]]; then
  bin_dir="$HOME/.local/bin"
else
  bin_dir="${LUNARCRUSH_HOME:-$HOME/.lunarcrush}/bin"
fi

exe="$bin_dir/lunarcrush"
mkdir -p "$bin_dir" || error "Failed to create $bin_dir"

# Download
url="${DOWNLOAD_BASE}/${target}"
info "${B}LunarCrush CLI${N}"
info "Downloading ${D}${url}${N}"

curl --fail --location --progress-bar -o "$exe" "$url" \
  || error "Download failed from $url"

chmod +x "$exe"

# Verify it runs
if ! "$exe" --help >/dev/null 2>&1; then
  rm -f "$exe"
  error "Downloaded binary failed to execute — your platform may not be supported"
fi

# Make sure bin_dir is on PATH — add to shell config if not
on_path=false
case ":${PATH}:" in
  *":${bin_dir}:"*) on_path=true ;;
esac

if ! $on_path; then
  shell_config=""
  shell_name=$(basename "${SHELL:-}")

  case "$shell_name" in
    zsh)  shell_config="$HOME/.zshrc" ;;
    bash)
      if [[ "$os" == "Darwin" ]]; then
        shell_config="$HOME/.bash_profile"
      else
        shell_config="$HOME/.bashrc"
      fi
      ;;
    fish) shell_config="$HOME/.config/fish/config.fish" ;;
  esac

  if [[ -n "$shell_config" ]] && ! grep -q "$bin_dir" "$shell_config" 2>/dev/null; then
    if [[ "$shell_name" == "fish" ]]; then
      echo -e "\n# LunarCrush CLI\nfish_add_path $bin_dir" >> "$shell_config"
    else
      echo -e "\n# LunarCrush CLI\nexport PATH=\"$bin_dir:\$PATH\"" >> "$shell_config"
    fi
  fi
fi

# Done
version=$("$exe" --version 2>/dev/null || echo "")
info ""
info "${G}Installed${N} lunarcrush${version:+ $version} to ${D}${exe}${N}"
info ""
info "Type command "lunarcrush" to get started. All of lunarcrush.ai is now available to you, your scripts, and your agents. See https://lunarcrush.ai/cli for documentation and examples."
