#!/bin/bash

# Vibes Install Script
# This script sets up the vibe command to be accessible from anywhere

set -e

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

echo -e "${BLUE}🌊 Vibes Installation${NC}"
echo

# Get the directory where the script is located
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_ROOT="$SCRIPT_DIR"

# Check if vibe.sh exists
if [ ! -f "$PROJECT_ROOT/tools/vibe.sh" ]; then
    echo -e "${RED}❌ Error: vibe.sh not found in tools directory${NC}"
    exit 1
fi

# Create bin directory if it doesn't exist
BIN_DIR="$PROJECT_ROOT/bin"
mkdir -p "$BIN_DIR"

# Create vibe command wrapper
cat > "$BIN_DIR/vibe" << EOF
#!/bin/bash
exec "$PROJECT_ROOT/tools/vibe.sh" "\$@"
EOF

# Make it executable
chmod +x "$BIN_DIR/vibe"

# Function to add to PATH
add_to_path() {
    local shell_config=$1
    local path_export="export PATH=\"$BIN_DIR:\$PATH\""
    
    if [ -f "$HOME/$shell_config" ]; then
        if ! grep -q "$BIN_DIR" "$HOME/$shell_config"; then
            echo -e "${YELLOW}Adding vibe to $shell_config...${NC}"
            echo "" >> "$HOME/$shell_config"
            echo "# Vibes CLI" >> "$HOME/$shell_config"
            echo "$path_export" >> "$HOME/$shell_config"
        else
            echo -e "${GREEN}✓ vibe already in $shell_config${NC}"
        fi
    fi
}

# Detect shell and add to appropriate config
SHELL_NAME=$(basename "$SHELL")

case "$SHELL_NAME" in
    bash)
        add_to_path ".bashrc"
        add_to_path ".bash_profile"
        ;;
    zsh)
        add_to_path ".zshrc"
        ;;
    fish)
        echo -e "${YELLOW}For fish shell, add this to ~/.config/fish/config.fish:${NC}"
        echo "set -x PATH $BIN_DIR \$PATH"
        ;;
    *)
        echo -e "${YELLOW}Unknown shell: $SHELL_NAME${NC}"
        echo -e "${YELLOW}Add this to your shell config:${NC}"
        echo "export PATH=\"$BIN_DIR:\$PATH\""
        ;;
esac

echo
echo -e "${GREEN}✅ Installation complete!${NC}"
echo
echo -e "${YELLOW}To use the vibe command immediately, run:${NC}"
echo "source ~/.$SHELL_NAME*rc"
echo
echo -e "${BLUE}Or open a new terminal and try:${NC}"
echo "vibe --help"
echo
echo -e "${GREEN}🌊 Happy vibing!${NC}"