#!/bin/bash

# Claude Config Global Installation Script
# This script installs claude-config CLI globally and sets up remote repository access

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Configuration
REPO_URL="https://github.com/greyhaven-ai/claude-code-config.git"
INSTALL_DIR="$HOME/.claude-config"
BIN_DIR="$HOME/.local/bin"
CONFIG_DIR="$HOME/.claude"

echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
echo -e "${BLUE}       Claude Config - Global Installation Script          ${NC}"
echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
echo

# Check if git is installed
if ! command -v git &> /dev/null; then
    echo -e "${RED}Error: git is not installed. Please install git first.${NC}"
    exit 1
fi

# Check if Python 3 is installed
if ! command -v python3 &> /dev/null; then
    echo -e "${RED}Error: Python 3 is not installed. Please install Python 3 first.${NC}"
    exit 1
fi

# Create directories if they don't exist
echo -e "${YELLOW}Creating directories...${NC}"
mkdir -p "$BIN_DIR"
mkdir -p "$CONFIG_DIR"

# Clone or update repository
if [ -d "$INSTALL_DIR" ]; then
    echo -e "${YELLOW}Updating existing installation...${NC}"
    cd "$INSTALL_DIR"
    git pull origin main
else
    echo -e "${YELLOW}Cloning repository...${NC}"
    git clone "$REPO_URL" "$INSTALL_DIR"
fi

# Create wrapper script
echo -e "${YELLOW}Creating wrapper script...${NC}"
cat > "$BIN_DIR/claude-config" << 'EOF'
#!/bin/bash
# Claude Config CLI Wrapper
# Auto-generated by install.sh

CLAUDE_CONFIG_HOME="$HOME/.claude-config"
CLAUDE_CONFIG_SCRIPT="$CLAUDE_CONFIG_HOME/claude-config"

# Check if installation exists
if [ ! -f "$CLAUDE_CONFIG_SCRIPT" ]; then
    echo "Error: Claude Config not found at $CLAUDE_CONFIG_HOME"
    echo "Please run the installation script again."
    exit 1
fi

# Export environment variable for the CLI to know it's globally installed
export CLAUDE_CONFIG_GLOBAL=1
export CLAUDE_CONFIG_HOME

# Execute the actual CLI with all arguments
exec python3 "$CLAUDE_CONFIG_SCRIPT" "$@"
EOF

# Make wrapper executable
chmod +x "$BIN_DIR/claude-config"

# Check if bin directory is in PATH
if [[ ":$PATH:" != *":$BIN_DIR:"* ]]; then
    echo -e "${YELLOW}Adding $BIN_DIR to PATH...${NC}"
    
    # Detect shell and update appropriate config file
    SHELL_NAME=$(basename "$SHELL")
    
    case "$SHELL_NAME" in
        bash)
            RC_FILE="$HOME/.bashrc"
            ;;
        zsh)
            RC_FILE="$HOME/.zshrc"
            ;;
        fish)
            RC_FILE="$HOME/.config/fish/config.fish"
            ;;
        *)
            RC_FILE="$HOME/.profile"
            ;;
    esac
    
    # Add to PATH
    echo "" >> "$RC_FILE"
    echo "# Claude Config CLI" >> "$RC_FILE"
    
    if [ "$SHELL_NAME" = "fish" ]; then
        echo "set -gx PATH \$PATH $BIN_DIR" >> "$RC_FILE"
    else
        echo "export PATH=\"\$PATH:$BIN_DIR\"" >> "$RC_FILE"
    fi
    
    echo -e "${GREEN}Added $BIN_DIR to PATH in $RC_FILE${NC}"
    echo -e "${YELLOW}Please restart your terminal or run: source $RC_FILE${NC}"
fi

# Create update script
cat > "$BIN_DIR/claude-config-update" << 'EOF'
#!/bin/bash
# Update Claude Config from repository

CLAUDE_CONFIG_HOME="$HOME/.claude-config"

echo "Updating Claude Config..."
cd "$CLAUDE_CONFIG_HOME" || exit 1

# Store current branch
CURRENT_BRANCH=$(git branch --show-current)

# Pull latest changes
git pull origin "$CURRENT_BRANCH"

echo "Claude Config updated successfully!"
EOF

chmod +x "$BIN_DIR/claude-config-update"

# Installation complete
echo
echo -e "${GREEN}═══════════════════════════════════════════════════════════${NC}"
echo -e "${GREEN}       Installation Complete!                              ${NC}"
echo -e "${GREEN}═══════════════════════════════════════════════════════════${NC}"
echo
echo -e "${GREEN}Claude Config has been installed to: $INSTALL_DIR${NC}"
echo -e "${GREEN}Executable linked to: $BIN_DIR/claude-config${NC}"
echo
echo -e "${BLUE}Available commands:${NC}"
echo "  claude-config              - Main CLI tool"
echo "  claude-config-update       - Update from repository"
echo
echo -e "${BLUE}Quick start:${NC}"
echo "  claude-config --help       - Show help"
echo "  claude-config init         - Initialize in current directory"
echo "  claude-config list-presets - Show available presets"
echo "  claude-config preset recommended - Apply recommended preset"
echo
echo -e "${YELLOW}Note: If 'claude-config' is not found, restart your terminal or run:${NC}"
echo -e "${YELLOW}  source $RC_FILE${NC}"