#!/bin/bash

# 核心MCP服务测试脚本
# 测试最重要的几个MCP服务

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 测试核心服务
test_core_services() {
    log_info "启动核心MCP服务测试..."
    
    # 创建简化的Docker Compose配置
    cat > docker-compose-core-test.yml << 'EOF'
name: xiaohongshu-core-mcp-test

services:
  # GitHub MCP (已配置)
  mcp-github:
    image: node:18-alpine
    container_name: mcp-github-test
    working_dir: /app
    environment:
      - GITHUB_PERSONAL_ACCESS_TOKEN=${GITHUB_PERSONAL_ACCESS_TOKEN}
    command: >
      sh -c "
        npm install -g @modelcontextprotocol/server-github &&
        echo 'GitHub MCP服务启动成功' &&
        npx @modelcontextprotocol/server-github
      "
    restart: unless-stopped
    networks:
      - mcp-test-network

  # Memory MCP
  mcp-memory:
    image: node:18-alpine
    container_name: mcp-memory-test
    working_dir: /app
    volumes:
      - ./mcp-test-data/memory:/app/data
    command: >
      sh -c "
        npm install -g @modelcontextprotocol/server-memory &&
        echo 'Memory MCP服务启动成功' &&
        npx @modelcontextprotocol/server-memory
      "
    restart: unless-stopped
    networks:
      - mcp-test-network

  # Filesystem MCP
  mcp-filesystem:
    image: node:18-alpine
    container_name: mcp-filesystem-test
    working_dir: /app
    volumes:
      - ./mcp-test-data/filesystem:/app/allowed
    command: >
      sh -c "
        npm install -g @modelcontextprotocol/server-filesystem &&
        echo 'Filesystem MCP服务启动成功' &&
        npx @modelcontextprotocol/server-filesystem /app/allowed
      "
    restart: unless-stopped
    networks:
      - mcp-test-network

  # Time MCP (无需API密钥)
  mcp-time:
    image: python:3.11-alpine
    container_name: mcp-time-test
    working_dir: /app
    command: >
      sh -c "
        pip install mcp-server-time &&
        echo 'Time MCP服务启动成功' &&
        python -m mcp_server_time
      "
    restart: unless-stopped
    networks:
      - mcp-test-network

networks:
  mcp-test-network:
    driver: bridge
EOF

    # 创建测试数据目录
    mkdir -p mcp-test-data/{memory,filesystem}
    echo "测试文件" > mcp-test-data/filesystem/test.txt
    
    log_info "启动核心服务..."
    docker-compose -f docker-compose-core-test.yml up -d
    
    # 等待服务启动
    log_info "等待服务启动 (30秒)..."
    sleep 30
    
    # 检查服务状态
    log_info "检查服务状态..."
    docker-compose -f docker-compose-core-test.yml ps
    
    # 测试每个服务
    test_service_health "mcp-github-test" "GitHub MCP"
    test_service_health "mcp-memory-test" "Memory MCP"
    test_service_health "mcp-filesystem-test" "Filesystem MCP"
    test_service_health "mcp-time-test" "Time MCP"
    
    log_success "核心服务测试完成！"
}

# 测试服务健康状态
test_service_health() {
    local container_name="$1"
    local service_name="$2"
    
    if docker ps | grep -q "$container_name"; then
        if docker logs "$container_name" 2>&1 | grep -q "启动成功\|listening\|ready\|started"; then
            log_success "$service_name 运行正常"
        else
            log_warning "$service_name 可能存在问题，查看日志："
            docker logs "$container_name" --tail=10
        fi
    else
        log_error "$service_name 未运行"
    fi
}

# 清理测试环境
cleanup_test() {
    log_info "清理测试环境..."
    
    docker-compose -f docker-compose-core-test.yml down -v 2>/dev/null || true
    rm -rf mcp-test-data
    rm -f docker-compose-core-test.yml
    
    log_success "测试环境清理完成"
}

# 显示测试结果
show_test_results() {
    echo ""
    log_info "=== 测试结果总结 ==="
    echo ""
    
    # 检查GitHub MCP配置
    if grep -q "github_pat_" .env; then
        log_success "✅ GitHub MCP - 已配置API密钥"
    else
        log_warning "⚠️  GitHub MCP - 需要配置API密钥"
    fi
    
    log_success "✅ Memory MCP - 无需API密钥，可直接使用"
    log_success "✅ Filesystem MCP - 无需API密钥，可直接使用"
    log_success "✅ Time MCP - 无需API密钥，可直接使用"
    
    echo ""
    log_info "=== 下一步建议 ==="
    echo ""
    echo "1. 🔑 配置更多API密钥以启用高级功能："
    echo "   - Brave Search API (网络搜索)"
    echo "   - Google APIs (Drive, Maps)"
    echo "   - Slack API (团队协作)"
    echo ""
    echo "2. 🚀 启动完整的20个MCP服务："
    echo "   ./manage-top20-mcp.sh start"
    echo ""
    echo "3. 📖 查看完整配置指南："
    echo "   cat TOP20_MCP_SETUP_GUIDE.md"
}

# 主函数
main() {
    case "${1:-test}" in
        test)
            test_core_services
            show_test_results
            ;;
        cleanup)
            cleanup_test
            ;;
        help|--help|-h)
            echo "核心MCP服务测试脚本"
            echo ""
            echo "用法: $0 [命令]"
            echo ""
            echo "命令:"
            echo "  test      运行核心服务测试 (默认)"
            echo "  cleanup   清理测试环境"
            echo "  help      显示帮助信息"
            ;;
        *)
            log_error "未知命令: $1"
            exit 1
            ;;
    esac
}

# 运行主函数
main "$@"
