#!/bin/bash

# 🚀 MCP服务生态系统快速启动脚本
# 基于awesome-mcp-servers项目的一键启动解决方案

set -e

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

# 显示欢迎信息
show_welcome() {
    clear
    echo -e "${PURPLE}"
    cat << 'EOF'
╔══════════════════════════════════════════════════════════════╗
║                🎯 MCP服务生态系统快速启动                     ║
║                                                              ║
║  基于 awesome-mcp-servers 项目的模块化MCP服务管理平台        ║
║  支持小红书投放、代码管理、知识管理等多种应用场景             ║
╚══════════════════════════════════════════════════════════════╝
EOF
    echo -e "${NC}"
    echo ""
}

# 检查系统环境
check_environment() {
    echo -e "${CYAN}[1/4]${NC} 检查系统环境..."
    
    # 检查Docker
    if ! command -v docker &> /dev/null; then
        echo -e "${RED}❌ Docker 未安装${NC}"
        echo "请先安装Docker: https://docs.docker.com/get-docker/"
        exit 1
    fi
    
    # 检查Docker Compose
    if ! command -v docker-compose &> /dev/null; then
        echo -e "${RED}❌ Docker Compose 未安装${NC}"
        echo "请先安装Docker Compose"
        exit 1
    fi
    
    # 检查Docker服务状态
    if ! docker info &> /dev/null; then
        echo -e "${RED}❌ Docker 服务未启动${NC}"
        echo "请启动Docker服务"
        exit 1
    fi
    
    echo -e "${GREEN}✅ 系统环境检查通过${NC}"
}

# 检查项目文件
check_project_files() {
    echo -e "${CYAN}[2/4]${NC} 检查项目文件..."
    
    local required_files=(
        "docker-compose.simple.yml"
        "mcp-manager.sh"
        ".env"
        "mcp-dashboard/index.html"
    )
    
    for file in "${required_files[@]}"; do
        if [ ! -f "$file" ]; then
            echo -e "${RED}❌ 缺少文件: $file${NC}"
            exit 1
        fi
    done
    
    echo -e "${GREEN}✅ 项目文件检查通过${NC}"
}

# 启动服务
start_services() {
    echo -e "${CYAN}[3/4]${NC} 启动MCP服务..."
    
    # 停止可能存在的旧服务
    docker-compose -f docker-compose.simple.yml down &> /dev/null || true
    
    # 启动新服务
    echo "正在启动服务容器..."
    docker-compose -f docker-compose.simple.yml up -d
    
    # 等待服务启动
    echo "等待服务初始化..."
    sleep 10
    
    echo -e "${GREEN}✅ MCP服务启动完成${NC}"
}

# 验证服务状态
verify_services() {
    echo -e "${CYAN}[4/4]${NC} 验证服务状态..."
    
    # 检查容器状态
    local running_containers=$(docker-compose -f docker-compose.simple.yml ps --services --filter "status=running" | wc -l)
    local total_containers=6
    
    if [ "$running_containers" -eq "$total_containers" ]; then
        echo -e "${GREEN}✅ 所有服务运行正常 ($running_containers/$total_containers)${NC}"
    else
        echo -e "${YELLOW}⚠️ 部分服务可能未正常启动 ($running_containers/$total_containers)${NC}"
    fi
    
    # 检查Web面板
    if curl -s http://localhost:8080 > /dev/null; then
        echo -e "${GREEN}✅ Web控制面板可访问${NC}"
    else
        echo -e "${YELLOW}⚠️ Web控制面板暂时不可访问${NC}"
    fi
}

# 显示服务信息
show_service_info() {
    echo ""
    echo -e "${PURPLE}🎉 MCP服务生态系统启动成功！${NC}"
    echo ""
    echo -e "${BLUE}📊 服务概览:${NC}"
    echo "  • Memory MCP - 知识图谱记忆系统"
    echo "  • Filesystem MCP - 文件系统操作"
    echo "  • Git MCP - 版本控制管理"
    echo "  • GitHub MCP - 代码仓库管理"
    echo "  • Brave Search MCP - 网络搜索引擎"
    echo "  • Web Dashboard - 可视化控制面板"
    echo ""
    echo -e "${BLUE}🌐 访问地址:${NC}"
    echo "  • Web控制面板: http://localhost:8080"
    echo ""
    echo -e "${BLUE}🔧 管理命令:${NC}"
    echo "  • 查看状态: docker-compose -f docker-compose.simple.yml ps"
    echo "  • 查看日志: docker logs mcp-memory-simple"
    echo "  • 重启服务: docker-compose -f docker-compose.simple.yml restart"
    echo "  • 停止服务: docker-compose -f docker-compose.simple.yml down"
    echo ""
    echo -e "${BLUE}📚 文档资源:${NC}"
    echo "  • 完整指南: cat MCP_ECOSYSTEM_GUIDE.md"
    echo "  • API配置: cat API_SETUP_GUIDE.md"
    echo "  • 部署报告: cat DEPLOYMENT_SUCCESS_REPORT.md"
    echo ""
    echo -e "${YELLOW}💡 下一步建议:${NC}"
    echo "  1. 访问 http://localhost:8080 查看控制面板"
    echo "  2. 配置 Brave Search API 密钥解锁搜索功能"
    echo "  3. 根据需要添加更多专业服务"
    echo "  4. 集成 Claude Desktop 使用MCP服务"
    echo ""
}

# 打开控制面板
open_dashboard() {
    echo -e "${CYAN}🚀 正在打开Web控制面板...${NC}"
    
    # 根据操作系统打开浏览器
    if command -v open &> /dev/null; then
        # macOS
        open http://localhost:8080
    elif command -v xdg-open &> /dev/null; then
        # Linux
        xdg-open http://localhost:8080
    elif command -v start &> /dev/null; then
        # Windows
        start http://localhost:8080
    else
        echo "请手动在浏览器中访问: http://localhost:8080"
    fi
}

# 主函数
main() {
    show_welcome
    check_environment
    check_project_files
    start_services
    verify_services
    show_service_info
    
    # 询问是否打开控制面板
    echo -n "是否现在打开Web控制面板? (y/n): "
    read -r response
    if [[ "$response" =~ ^[Yy]$ ]]; then
        open_dashboard
    fi
    
    echo ""
    echo -e "${GREEN}🎊 部署完成！享受你的MCP服务生态系统吧！${NC}"
}

# 运行主函数
main "$@"
