#!/bin/bash
# Setup MCP Servers for ClaudeTools
# This script helps configure MCP servers for Claude Code

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
MCP_CONFIG="$PROJECT_ROOT/.mcp.json"
MCP_EXAMPLE="$PROJECT_ROOT/.mcp.json.example"

echo "[INFO] MCP Server Setup Script"
echo "[INFO] Project Root: $PROJECT_ROOT"
echo ""

# Check if Node.js is installed
if ! command -v node &> /dev/null; then
    echo "[ERROR] Node.js is not installed"
    echo "[INFO] Please install Node.js from https://nodejs.org/"
    exit 1
fi

NODE_VERSION=$(node --version)
echo "[OK] Node.js installed: $NODE_VERSION"

# Check if npx is available
if ! command -v npx &> /dev/null; then
    echo "[ERROR] npx is not available"
    echo "[INFO] Please ensure npm is installed correctly"
    exit 1
fi

echo "[OK] npx is available"
echo ""

# Check if .mcp.json exists
if [ -f "$MCP_CONFIG" ]; then
    echo "[INFO] MCP configuration already exists: $MCP_CONFIG"
    read -p "Do you want to overwrite it? (y/N): " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        echo "[INFO] Keeping existing configuration"
        exit 0
    fi
fi

# Copy example configuration
if [ ! -f "$MCP_EXAMPLE" ]; then
    echo "[ERROR] Example configuration not found: $MCP_EXAMPLE"
    exit 1
fi

cp "$MCP_EXAMPLE" "$MCP_CONFIG"
echo "[OK] Created MCP configuration from template"

# Prompt for GitHub token
echo ""
echo "[SETUP] GitHub MCP Server Configuration"
echo "[INFO] You need a GitHub Personal Access Token to use the GitHub MCP server"
echo "[INFO] Generate one at: https://github.com/settings/tokens"
echo "[INFO] Required scopes: repo, workflow, read:org, read:user"
echo ""
read -p "Enter your GitHub Personal Access Token (or press Enter to skip): " GITHUB_TOKEN

if [ -n "$GITHUB_TOKEN" ]; then
    # Update token in configuration (cross-platform sed)
    if [[ "$OSTYPE" == "darwin"* ]]; then
        # macOS
        sed -i '' "s/YOUR_GITHUB_TOKEN_HERE/$GITHUB_TOKEN/g" "$MCP_CONFIG"
    else
        # Linux/Git Bash
        sed -i "s/YOUR_GITHUB_TOKEN_HERE/$GITHUB_TOKEN/g" "$MCP_CONFIG"
    fi
    echo "[OK] GitHub token configured"
else
    echo "[WARNING] GitHub MCP server will not work without a token"
    echo "[INFO] You can add it later by editing $MCP_CONFIG"
fi

# Test MCP server packages
echo ""
echo "[TEST] Verifying MCP server packages..."

echo "[TEST] Testing Sequential Thinking MCP server..."
if npx -y @modelcontextprotocol/server-sequential-thinking --version &> /dev/null; then
    echo "[OK] Sequential Thinking MCP server is available"
else
    echo "[WARNING] Sequential Thinking MCP server test failed (may work anyway)"
fi

echo "[TEST] Testing Filesystem MCP server..."
if npx -y @modelcontextprotocol/server-filesystem --help &> /dev/null; then
    echo "[OK] Filesystem MCP server is available"
else
    echo "[WARNING] Filesystem MCP server test failed (may work anyway)"
fi

if [ -n "$GITHUB_TOKEN" ]; then
    echo "[TEST] Testing GitHub MCP server..."
    if npx -y @modelcontextprotocol/server-github --version &> /dev/null; then
        echo "[OK] GitHub MCP server is available"
    else
        echo "[WARNING] GitHub MCP server test failed (may work anyway)"
    fi
fi

echo ""
echo "[SUCCESS] MCP servers configured successfully!"
echo ""
echo "[NEXT STEPS]"
echo "1. Restart Claude Code completely (quit and relaunch)"
echo "2. Open the ClaudeTools project in Claude Code"
echo "3. Test MCP servers:"
echo "   - Sequential Thinking: Ask Claude to 'use sequential thinking to analyze X'"
echo "   - Filesystem: Ask Claude to 'list Python files in the api directory'"
echo "   - GitHub: Ask Claude to 'list my recent GitHub repositories'"
echo ""
echo "[DOCUMENTATION]"
echo "See D:\\ClaudeTools\\MCP_SERVERS.md for complete documentation"
echo ""
