#!/bin/bash
#
# ELIDA Client Configuration Deployment Script for Linux
#
# This script configures AI coding tools to route through ELIDA.
#
# Usage:
#   ./deploy-elida-config.sh                    # Uses default ELIDA host
#   ELIDA_HOST="http://proxy:8080" ./deploy-elida-config.sh
#
# For enterprise deployment:
#   - Run as part of user provisioning
#   - Include in dotfiles management (chezmoi, yadm, etc.)
#   - Deploy via configuration management (Ansible, Chef, Puppet)
#

set -e

# Configuration - Change this to your ELIDA proxy address
ELIDA_HOST="${ELIDA_HOST:-http://elida.corp.local:8080}"

echo "=========================================="
echo "  ELIDA Client Configuration Deployment"
echo "=========================================="
echo ""
echo "ELIDA Host: $ELIDA_HOST"
echo "User: $(whoami)"
echo ""

# -----------------------------------------------------------------------------
# Claude Code CLI
# -----------------------------------------------------------------------------
echo "Configuring Claude Code CLI..."
CLAUDE_DIR="$HOME/.claude"
mkdir -p "$CLAUDE_DIR"
cat > "$CLAUDE_DIR/settings.json" << EOF
{
  "env": {
    "ANTHROPIC_BASE_URL": "$ELIDA_HOST"
  }
}
EOF
echo "  Created: $CLAUDE_DIR/settings.json"

# -----------------------------------------------------------------------------
# Continue.dev (VS Code Extension)
# -----------------------------------------------------------------------------
echo "Configuring Continue.dev..."
CONTINUE_DIR="$HOME/.continue"
mkdir -p "$CONTINUE_DIR"
cat > "$CONTINUE_DIR/config.yaml" << EOF
# ELIDA-managed Continue.dev configuration
# Generated by deploy-elida-config.sh

models:
  # Anthropic Claude
  - name: "claude-3-opus"
    provider: "anthropic"
    apiBase: "$ELIDA_HOST"
    apiKey: "\${ANTHROPIC_API_KEY}"

  - name: "claude-3-sonnet"
    provider: "anthropic"
    apiBase: "$ELIDA_HOST"
    apiKey: "\${ANTHROPIC_API_KEY}"

  # OpenAI GPT
  - name: "gpt-4"
    provider: "openai"
    apiBase: "$ELIDA_HOST/v1"
    apiKey: "\${OPENAI_API_KEY}"

  - name: "gpt-4-turbo"
    provider: "openai"
    apiBase: "$ELIDA_HOST/v1"
    apiKey: "\${OPENAI_API_KEY}"

  # Mistral
  - name: "codestral"
    provider: "mistral"
    apiBase: "$ELIDA_HOST/v1"
    apiKey: "\${MISTRAL_API_KEY}"

# Tab autocomplete model
tabAutocompleteModel:
  provider: "openai"
  apiBase: "$ELIDA_HOST/v1"
  apiKey: "\${OPENAI_API_KEY}"
  model: "gpt-4"
EOF
echo "  Created: $CONTINUE_DIR/config.yaml"

# -----------------------------------------------------------------------------
# Shell Profile
# -----------------------------------------------------------------------------
echo "Configuring shell environment..."

# Determine shell profile
if [ -n "$ZSH_VERSION" ] || [ -f "$HOME/.zshrc" ]; then
    SHELL_PROFILES=("$HOME/.zshrc")
elif [ -n "$BASH_VERSION" ] || [ -f "$HOME/.bashrc" ]; then
    SHELL_PROFILES=("$HOME/.bashrc")
else
    SHELL_PROFILES=("$HOME/.profile")
fi

# Also add .bashrc if it exists (many systems source both)
if [ -f "$HOME/.bashrc" ] && [[ ! " ${SHELL_PROFILES[*]} " =~ " $HOME/.bashrc " ]]; then
    SHELL_PROFILES+=("$HOME/.bashrc")
fi

for SHELL_PROFILE in "${SHELL_PROFILES[@]}"; do
    # Remove old ELIDA config if present
    if grep -q "# ELIDA AI Proxy Configuration" "$SHELL_PROFILE" 2>/dev/null; then
        # Create temp file without ELIDA block
        sed '/# ELIDA AI Proxy Configuration/,/# End ELIDA/d' "$SHELL_PROFILE" > "$SHELL_PROFILE.tmp"
        mv "$SHELL_PROFILE.tmp" "$SHELL_PROFILE"
    fi

    # Add new configuration
    cat >> "$SHELL_PROFILE" << EOF

# ELIDA AI Proxy Configuration
# Managed by deploy-elida-config.sh - Do not edit manually
export ANTHROPIC_BASE_URL="$ELIDA_HOST"
export OPENAI_BASE_URL="$ELIDA_HOST/v1"
export OPENAI_API_BASE="$ELIDA_HOST/v1"
export MISTRAL_API_BASE="$ELIDA_HOST"
export LLM_PROXY_URL="$ELIDA_HOST"
# End ELIDA
EOF
    echo "  Updated: $SHELL_PROFILE"
done

# -----------------------------------------------------------------------------
# Systemd User Environment (for GUI apps on modern Linux)
# -----------------------------------------------------------------------------
if command -v systemctl &> /dev/null && [ -d "$HOME/.config" ]; then
    echo "Configuring systemd user environment..."
    mkdir -p "$HOME/.config/environment.d"
    cat > "$HOME/.config/environment.d/elida.conf" << EOF
# ELIDA AI Proxy Configuration
ANTHROPIC_BASE_URL=$ELIDA_HOST
OPENAI_BASE_URL=$ELIDA_HOST/v1
OPENAI_API_BASE=$ELIDA_HOST/v1
MISTRAL_API_BASE=$ELIDA_HOST
LLM_PROXY_URL=$ELIDA_HOST
EOF
    echo "  Created: $HOME/.config/environment.d/elida.conf"
fi

# -----------------------------------------------------------------------------
# XDG Environment (for desktop environments)
# -----------------------------------------------------------------------------
if [ -d "$HOME/.config" ]; then
    # Some desktop environments read from ~/.pam_environment or ~/.config/plasma-workspace/env/
    if [ -d "$HOME/.config/plasma-workspace/env" ]; then
        cat > "$HOME/.config/plasma-workspace/env/elida.sh" << EOF
#!/bin/sh
export ANTHROPIC_BASE_URL="$ELIDA_HOST"
export OPENAI_BASE_URL="$ELIDA_HOST/v1"
export MISTRAL_API_BASE="$ELIDA_HOST"
EOF
        chmod +x "$HOME/.config/plasma-workspace/env/elida.sh"
        echo "  Created: $HOME/.config/plasma-workspace/env/elida.sh"
    fi
fi

# -----------------------------------------------------------------------------
# Summary
# -----------------------------------------------------------------------------
echo ""
echo "=========================================="
echo "  Deployment Complete!"
echo "=========================================="
echo ""
echo "Configured:"
echo "  - Claude Code CLI (~/.claude/settings.json)"
echo "  - Continue.dev (~/.continue/config.yaml)"
echo "  - Shell environment (${SHELL_PROFILES[*]})"
if [ -f "$HOME/.config/environment.d/elida.conf" ]; then
    echo "  - Systemd user environment (~/.config/environment.d/elida.conf)"
fi
echo ""
echo "To apply changes:"
echo "  1. Run: source ~/.bashrc  (or ~/.zshrc)"
echo "  2. Log out and back in (for GUI apps)"
echo "  3. Restart any running AI coding tools"
echo ""
echo "Verify configuration:"
echo "  curl $ELIDA_HOST/health"
