#!/bin/bash
set -euo pipefail

trap 'error "Install failed at line $LINENO. Re-run with: bash -x <(curl -fsSL https://alignment-hive.com/install.sh)"' ERR

# alignment-hive installer
# Usage: curl -fsSL https://alignment-hive.com/install.sh | bash

# --- Configuration ---

CLIENT_ID="${ALIGNMENT_HIVE_CLIENT_ID:-client_01KE10CZ6FFQB9TR2NVBQJ4AKV}"
WORKOS_API="https://api.workos.com/user_management"
AUTH_DIR="$HOME/.alignment-hive"

resolve_auth_file() {
  local env_path="${ALIGNMENT_HIVE_AUTH_FILE:-}"
  if [ -n "$env_path" ]; then
    case "$env_path" in
      "~/"*) echo "$HOME/${env_path#\~/}" ;;
      *)     echo "$env_path" ;;
    esac
  else
    echo "$AUTH_DIR/auth.json"
  fi
}

# Claude Code's config dir, where `claude plugin` records marketplaces and installs.
resolve_claude_config_dir() {
  local env_path="${CLAUDE_CONFIG_DIR:-}"
  case "$env_path" in
    "")    echo "$HOME/.claude" ;;
    "~/"*) echo "$HOME/${env_path#\~/}" ;;
    *)     echo "$env_path" ;;
  esac
}

AUTH_FILE="$(resolve_auth_file)"
JQ_CACHE_DIR="$HOME/.cache/alignment-hive"
JQ_BINARY="$JQ_CACHE_DIR/jq"
CLAUDE_PLUGINS_DIR="$(resolve_claude_config_dir)/plugins"
KNOWN_MARKETPLACES="$CLAUDE_PLUGINS_DIR/known_marketplaces.json"
INSTALLED_PLUGINS="$CLAUDE_PLUGINS_DIR/installed_plugins.json"
JQ=""
# The first hive-cli release published with SHA256SUMS. Older ones have none, so their binary
# installs unverified; from this one on, a binary that can't be checked against it isn't installed.
FIRST_SUMMED_CLI_VERSION="0.2.15"

# --- Output helpers ---

BOLD=""
GREEN=""
YELLOW=""
RED=""
RESET=""

if [ -t 1 ] || [ -t 2 ]; then
  BOLD=$(tput bold 2>/dev/null || true)
  GREEN=$(tput setaf 2 2>/dev/null || true)
  YELLOW=$(tput setaf 3 2>/dev/null || true)
  RED=$(tput setaf 1 2>/dev/null || true)
  RESET=$(tput sgr0 2>/dev/null || true)
fi

info()    { echo "  ${BOLD}$1${RESET}"; }
success() { echo "  ${GREEN}$1${RESET}"; }
warn()    { echo "  ${YELLOW}$1${RESET}"; }
error()   { echo "  ${RED}$1${RESET}" >&2; }

# The SHA-256 of file $1; fails when there's no sha256sum or shasum.
sha256() {
  local sum
  if command -v sha256sum >/dev/null 2>&1; then sum=$(sha256sum < "$1") || return 1
  elif command -v shasum >/dev/null 2>&1; then sum=$(shasum -a 256 < "$1") || return 1
  else return 1
  fi
  echo "${sum%% *}"
}

# Whether version $1 is older than $2, both X.Y.Z; false when either isn't.
older() {
  [[ "$1" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] || return 1
  local a=$((10#${BASH_REMATCH[1]} * 1000000 + 10#${BASH_REMATCH[2]} * 1000 + 10#${BASH_REMATCH[3]}))
  [[ "$2" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]] || return 1
  local b=$((10#${BASH_REMATCH[1]} * 1000000 + 10#${BASH_REMATCH[2]} * 1000 + 10#${BASH_REMATCH[3]}))
  (( a < b ))
}

# Checks file $1, the release asset named $2, against the SHA256SUMS of the release at URL $3.
# When it doesn't pass, prints why and fails.
check_release_sum() {
  local sums expected actual
  sums=$(curl -fsSL "$3/SHA256SUMS" 2>/dev/null) || { echo "couldn't fetch the release's SHA256SUMS"; return 1; }
  expected=$(awk -v name="$2" '$2 == name { print $1 }' <<< "$sums")
  [ -n "$expected" ] || { echo "the release's SHA256SUMS doesn't list $2"; return 1; }
  actual=$(sha256 "$1") || { echo "found no sha256sum or shasum to check it with"; return 1; }
  [ "$actual" = "$expected" ] || { echo "it doesn't match the release's SHA256SUMS (expected $expected, got $actual)"; return 1; }
}

# --- Step 1: Check claude CLI ---

check_claude() {
  if ! command -v claude >/dev/null 2>&1; then
    error "Claude Code is not installed."
    echo "  Install it first: curl -fsSL https://claude.ai/install.sh | bash"
    exit 1
  fi
}

# --- Step 2: Bootstrap jq ---
# Adapted from plugins/autopilot/scripts/ensure-jq.sh

ensure_jq() {
  if command -v jq >/dev/null 2>&1; then
    JQ="jq"
    return 0
  fi

  if [ -x "$JQ_BINARY" ]; then
    JQ="$JQ_BINARY"
    return 0
  fi

  local os arch os_name arch_name
  os=$(uname -s | tr '[:upper:]' '[:lower:]')
  arch=$(uname -m)

  case "$os" in
    linux)  os_name="linux" ;;
    darwin) os_name="macos" ;;
    *)      error "Unsupported OS: $os"; exit 1 ;;
  esac

  case "$arch" in
    x86_64)        arch_name="amd64" ;;
    aarch64|arm64) arch_name="arm64" ;;
    *)             error "Unsupported architecture: $arch"; exit 1 ;;
  esac

  local url="https://github.com/jqlang/jq/releases/download/jq-1.7.1/jq-${os_name}-${arch_name}"
  # From the jq-1.7.1 release's sha256sum.txt.
  local expected
  case "${os_name}-${arch_name}" in
    linux-amd64) expected="5942c9b0934e510ee61eb3e30273f1b3fe2590df93933a93d7c58b81d19c8ff5" ;;
    linux-arm64) expected="4dd2d8a0661df0b22f1bb9a1f9830f06b6f3b8f7d91211a1ef5d7c4f06a8b4a5" ;;
    macos-amd64) expected="4155822bbf5ea90f5c79cf254665975eb4274d426d0709770c21774de5407443" ;;
    macos-arm64) expected="0bbe619e663e0de2c550be2fe0d240d076799d6f8a652b70fa04aea8a8362e8a" ;;
  esac

  info "Downloading jq..."
  mkdir -p "$JQ_CACHE_DIR"

  if ! curl -fSL "$url" -o "$JQ_BINARY" 2>/dev/null; then
    rm -f "$JQ_BINARY"
    error "Failed to download jq. Install jq manually and re-run."
    exit 1
  fi

  local actual
  if ! actual=$(sha256 "$JQ_BINARY"); then
    rm -f "$JQ_BINARY"
    error "Found no sha256sum or shasum to check the jq download with. Install jq manually and re-run."
    exit 1
  fi
  if [ "$actual" != "$expected" ]; then
    rm -f "$JQ_BINARY"
    error "Downloaded jq doesn't match its published checksum. Install jq manually and re-run."
    exit 1
  fi

  chmod +x "$JQ_BINARY"

  if ! "$JQ_BINARY" --version >/dev/null 2>&1; then
    rm -f "$JQ_BINARY"
    error "Downloaded jq binary is corrupt. Install jq manually."
    exit 1
  fi

  JQ="$JQ_BINARY"
  success "jq bootstrapped"
}

# --- Step 3: Add marketplace ---

add_marketplace() {
  if [ -f "$KNOWN_MARKETPLACES" ] && $JQ -e '."alignment-hive"' "$KNOWN_MARKETPLACES" >/dev/null 2>&1; then
    info "Marketplace already added"
    return 0
  fi

  info "Adding alignment-hive marketplace..."
  claude plugin marketplace add Crazytieguy/alignment-hive
  success "Marketplace added"
}

# --- Step 4: Enable auto-update ---

enable_auto_update() {
  if [ ! -f "$KNOWN_MARKETPLACES" ]; then
    warn "Marketplace file not found. Enable auto-update manually: /plugin > Marketplaces > alignment-hive > Enable auto-update"
    return 0
  fi

  if ! $JQ -e '."alignment-hive"' "$KNOWN_MARKETPLACES" >/dev/null 2>&1; then
    warn "alignment-hive not found in marketplaces. Enable auto-update manually."
    return 0
  fi

  if $JQ -e '."alignment-hive".autoUpdate == true' "$KNOWN_MARKETPLACES" >/dev/null 2>&1; then
    info "Auto-update already enabled"
    return 0
  fi

  info "Enabling auto-update..."
  local tmp
  tmp=$(mktemp "$CLAUDE_PLUGINS_DIR/known_marketplaces.XXXXXX")
  if $JQ '."alignment-hive".autoUpdate = true' "$KNOWN_MARKETPLACES" > "$tmp"; then
    mv "$tmp" "$KNOWN_MARKETPLACES"
    success "Auto-update enabled"
  else
    rm -f "$tmp"
    warn "Failed to enable auto-update. Enable manually: /plugin > Marketplaces > alignment-hive > Enable auto-update"
  fi
}

# --- Step 5: Install hive plugin ---

install_hive_plugin() {
  # Dev mode: plugin is loaded from the local repo, skip marketplace install
  if [ "${ALIGNMENT_HIVE_DEV:-}" = "1" ]; then
    info "Dev mode: skipping plugin install (using local plugin)"
    return 0
  fi

  # Always update marketplace first so the latest plugin list is available
  info "Updating marketplace..."
  claude plugin marketplace update alignment-hive 2>/dev/null || true

  if [ -f "$INSTALLED_PLUGINS" ] && $JQ -e '.plugins."hive@alignment-hive"' "$INSTALLED_PLUGINS" >/dev/null 2>&1; then
    info "Updating hive plugin..."
    claude plugin update hive@alignment-hive 2>/dev/null || true
    success "hive plugin up to date"
  else
    info "Installing hive plugin..."
    claude plugin install hive@alignment-hive
    success "hive plugin installed"
  fi
}

# --- Step 6: Install hive binary globally ---

install_hive_binary() {
  # Dev mode: compile and use dev binary
  if [ "${ALIGNMENT_HIVE_DEV:-}" = "1" ]; then
    local repo_root="${CLAUDE_PROJECT_DIR:-$(pwd)}"
    info "Building dev binary..."
    (cd "$repo_root" && bun run --filter '@alignment-hive/hive-cli' build:dev) || {
      warn "Dev binary build failed. Install may use stale binary."
    }
    if [ -x "$repo_root/.dev/hive" ]; then
      export PATH="$repo_root/.dev:$PATH"
      success "Using dev binary at $repo_root/.dev/hive"
    else
      info "Dev binary not found after build."
    fi
    return 0
  fi

  # Find plugin install path to read cli-version
  if [ ! -f "$INSTALLED_PLUGINS" ]; then
    warn "Cannot find installed plugins. Skipping binary install."
    return 0
  fi

  local plugin_path
  plugin_path=$($JQ -r '.plugins."hive@alignment-hive"[0].installPath // empty' "$INSTALLED_PLUGINS" 2>/dev/null)
  if [ -z "$plugin_path" ]; then
    warn "hive plugin path not found. Skipping binary install."
    return 0
  fi

  local version_file="$plugin_path/cli-version"
  if [ ! -f "$version_file" ]; then
    warn "cli-version not found. Skipping binary install."
    return 0
  fi

  local version
  version=$(cat "$version_file" | tr -d '[:space:]')
  if [ -z "$version" ]; then
    warn "cli-version is empty. Skipping binary install."
    return 0
  fi

  # Detect platform
  local os arch os_name arch_name
  os=$(uname -s | tr '[:upper:]' '[:lower:]')
  arch=$(uname -m)

  case "$os" in
    linux)  os_name="linux" ;;
    darwin) os_name="darwin" ;;
    *)      error "Unsupported OS: $os"; exit 1 ;;
  esac

  case "$arch" in
    x86_64)        arch_name="x64" ;;
    aarch64|arm64) arch_name="arm64" ;;
    *)             error "Unsupported architecture: $arch"; exit 1 ;;
  esac

  local cache_dir="$HOME/.cache/hive/v${version}"
  local binary="$cache_dir/hive"
  local target="${os_name}-${arch_name}"

  # Download if not already cached
  if [ ! -x "$binary" ]; then
    local binary_name="hive-cli-${target}"
    local download_url="https://github.com/Crazytieguy/alignment-hive/releases/download/hive-cli-v${version}/${binary_name}"

    # Downloaded beside the binary and moved into place once checked. bootstrap.sh clears
    # leftover .hive.tmp.* files.
    local tmp="$cache_dir/.hive.tmp.$$" problem

    info "Downloading hive binary v${version}..."
    mkdir -p "$cache_dir"

    if ! curl -fSL "$download_url" -o "$tmp" 2>/dev/null; then
      rm -f "$tmp"
      error "Failed to download hive binary v${version}."
      exit 1
    fi

    if older "$version" "$FIRST_SUMMED_CLI_VERSION"; then
      warn "hive-cli v${version} was released before SHA256SUMS; installing the binary unverified."
    elif ! problem=$(check_release_sum "$tmp" "$binary_name" "${download_url%/*}"); then
      rm -f "$tmp"
      error "Not installing hive binary v${version}: ${problem}."
      exit 1
    fi

    chmod +x "$tmp"
    mv "$tmp" "$binary"
  fi

  # Symlink to ~/.local/bin
  mkdir -p "$HOME/.local/bin"
  ln -sf "$binary" "$HOME/.local/bin/hive"

  # Ensure ~/.local/bin is in PATH
  if ! echo "$PATH" | tr ':' '\n' | grep -qx "$HOME/.local/bin"; then
    local shell_config=""
    case "${SHELL:-/bin/bash}" in
      */zsh)  shell_config="$HOME/.zshrc" ;;
      */bash) shell_config="$HOME/.bashrc" ;;
      */fish) shell_config="$HOME/.config/fish/config.fish" ;;
      *)      shell_config="$HOME/.profile" ;;
    esac

    if [ -n "$shell_config" ]; then
      local path_line='export PATH="$HOME/.local/bin:$PATH"'
      if ! grep -qF '.local/bin' "$shell_config" 2>/dev/null; then
        echo "$path_line" >> "$shell_config"
        warn "Added ~/.local/bin to PATH in $shell_config (restart your shell to activate)"
      fi
    fi
  fi

  success "hive binary installed to ~/.local/bin/hive"
}

# --- Auth helpers ---

# Written to a mktemp file (600 from creation) and moved into place, so the tokens are never
# readable by others. On a failure the auth file is left as it was.
save_auth() {
  local response="$1" authenticated_at="${2:-}" tmp
  mkdir -p "$AUTH_DIR"
  chmod 700 "$AUTH_DIR"
  tmp=$(mktemp "$AUTH_FILE.XXXXXX") || return 1
  if [ -n "$authenticated_at" ]; then
    echo "$response" | $JQ --argjson at "$authenticated_at" '. + {authenticated_at: $at}' > "$tmp" || { rm -f "$tmp"; return 1; }
  else
    echo "$response" > "$tmp"
  fi
  mv "$tmp" "$AUTH_FILE"
}

# --- Step 6: Check existing auth ---

check_auth() {
  if [ ! -f "$AUTH_FILE" ]; then
    return 1
  fi

  local refresh_token
  refresh_token=$($JQ -r '.refresh_token // empty' "$AUTH_FILE" 2>/dev/null)
  if [ -z "$refresh_token" ]; then
    return 1
  fi

  local response
  response=$(curl -s -X POST "$WORKOS_API/authenticate" \
    -d "grant_type=refresh_token" \
    -d "refresh_token=$refresh_token" \
    -d "client_id=$CLIENT_ID")

  if ! echo "$response" | $JQ -e '.access_token and .refresh_token and .user' >/dev/null 2>&1; then
    return 1
  fi

  local authenticated_at
  authenticated_at=$($JQ -r '.authenticated_at // empty' "$AUTH_FILE" 2>/dev/null || true)
  # A login this script can't save is lost (WorkOS has already rotated the refresh token), so
  # it falls through to signing in again.
  save_auth "$response" "$authenticated_at" || return 1

  local email
  email=$(echo "$response" | $JQ -r '.user.email // "unknown"')
  success "Authenticated as $email"
  return 0
}

# --- Step 7: Device auth flow ---


device_auth_flow() {
  local response
  response=$(curl -s -X POST "$WORKOS_API/authorize/device" \
    -d "client_id=$CLIENT_ID")

  local device_code user_code verification_uri interval expires_in
  device_code=$(echo "$response" | $JQ -r '.device_code // empty')
  user_code=$(echo "$response" | $JQ -r '.user_code // empty')
  verification_uri=$(echo "$response" | $JQ -r '.verification_uri_complete // empty')
  interval=$(echo "$response" | $JQ -r '.interval // 5')
  expires_in=$(echo "$response" | $JQ -r '.expires_in // 300')

  if [ -z "$device_code" ]; then
    error "Failed to start authentication."
    return 1
  fi

  info "Opening browser to confirm authentication..."
  echo "    $verification_uri"

  # Try to open browser
  open "$verification_uri" 2>/dev/null || xdg-open "$verification_uri" 2>/dev/null || wslview "$verification_uri" 2>/dev/null || true

  info "Confirm code ${BOLD}${GREEN}$user_code${RESET} in your browser, then wait..."

  # Poll for token
  local deadline=$((SECONDS + expires_in))
  while [ $SECONDS -lt $deadline ]; do
    sleep "$interval"

    local token_response
    token_response=$(curl -s -X POST "$WORKOS_API/authenticate" \
      -d "grant_type=urn:ietf:params:oauth:grant-type:device_code" \
      -d "device_code=$device_code" \
      -d "client_id=$CLIENT_ID")

    local err
    err=$(echo "$token_response" | $JQ -r '.error // empty')

    if [ -z "$err" ]; then
      if ! save_auth "$token_response" "$(date +%s)000"; then
        error "Couldn't save the login to $AUTH_FILE."
        return 1
      fi

      local email first_name
      email=$(echo "$token_response" | $JQ -r '.user.email // "unknown"')
      first_name=$(echo "$token_response" | $JQ -r '.user.first_name // empty')

      success "Authenticated as ${first_name:-$email} ($email)"
      return 0
    elif [ "$err" = "slow_down" ]; then
      interval=$((interval + 1))
    elif [ "$err" != "authorization_pending" ]; then
      error "Authentication failed: $err"
      return 1
    fi
  done

  error "Authentication timed out."
  return 1
}

# --- Main ---

main() {
  echo ""
  info "alignment-hive installer"
  echo ""

  check_claude
  ensure_jq
  add_marketplace
  enable_auto_update
  install_hive_plugin
  install_hive_binary

  echo ""

  # Reclaim stdin from pipe (curl | bash) so interactive prompts work
  exec < /dev/tty

  AUTHED=false
  if check_auth; then
    AUTHED=true
  else
    printf "  Authenticate to enable session sharing? (Requires an alignment-hive invite) [Y/n] "
    read -r REPLY
    case "$REPLY" in
      [Nn]*)
        info "You can authenticate later by re-running this script."
        ;;
      *)
        echo ""
        if device_auth_flow; then
          AUTHED=true
        else
          warn "Authentication failed. Check your email for an alignment-hive invite,"
          warn "or contact yoav.tzfati@gmail.com to request one."
          echo ""
          info "Next steps:"
          echo "    1. Open Claude Code in a project directory"
          echo "    2. Run /hive:align"
          echo ""
          exit 0
        fi
        ;;
    esac
  fi

  echo ""

  # Consent + project setup (handled entirely by the CLI)
  if [ "$AUTHED" = true ] && command -v hive >/dev/null 2>&1; then
    if ! hive consent setup; then
      warn "Consent setup failed. You can retry later with: hive consent setup"
    fi
    echo ""
    echo "  Sessions are uploaded after a 24-hour review period."
    echo "  Run 'hive upload --help' to learn about reviewing, excluding, and snoozing uploads."
    echo ""
  fi

  info "Next steps:"
  echo "    1. Open Claude Code in a project directory"
  echo "    2. Run /hive:align"
  echo ""

  # Point stdin to /dev/null so bash exits cleanly (not /dev/tty which blocks)
  exec < /dev/null
}

main "$@"
