#!/bin/bash

# 全自动Debug MCP服务启动脚本
# 包含自动截图分析、精准局部修改、获取控制台日志等功能

set -e

echo "🚀 启动全自动Debug MCP服务..."

# 检查Docker是否运行
if ! docker info > /dev/null 2>&1; then
    echo "❌ Docker未运行，请先启动Docker"
    exit 1
fi

# 创建网络（如果不存在）
if ! docker network ls | grep -q mcp-network; then
    echo "🌐 创建MCP网络..."
    docker network create mcp-network
fi

# 进入项目目录
cd "$(dirname "$0")/.."

# 构建并启动Debug MCP服务
echo "🔨 构建Debug MCP服务..."
docker-compose -f docker/docker-compose.debug-automation.yml build

echo "🚀 启动Debug MCP服务..."
docker-compose -f docker/docker-compose.debug-automation.yml up -d

# 等待服务启动
echo "⏳ 等待服务启动..."
sleep 10

# 检查服务状态
if docker-compose -f docker/docker-compose.debug-automation.yml ps | grep -q "Up"; then
    echo "✅ Debug MCP服务启动成功！"
    echo ""
    echo "📊 服务信息:"
    echo "  - 容器名称: debug-automation-mcp"
    echo "  - 端口: 8080"
    echo "  - 调试截图目录: mcp-debug-automation/debug_screenshots"
    echo "  - 调试报告目录: mcp-debug-automation/debug_reports"
    echo ""
    echo "🔧 可用工具:"
    echo "  - start_debug_browser: 启动调试浏览器"
    echo "  - auto_screenshot_analysis: 自动截图分析"
    echo "  - navigate_with_debug: 导航并调试"
    echo "  - get_console_logs: 获取控制台日志"
    echo "  - get_network_logs: 获取网络日志"
    echo "  - analyze_element: 分析页面元素"
    echo "  - precise_element_modification: 精准元素修改"
    echo "  - generate_debug_report: 生成调试报告"
    echo "  - close_debug_browser: 关闭调试浏览器"
    echo ""
    echo "📝 查看日志: docker-compose -f docker/docker-compose.debug-automation.yml logs -f"
    echo "🛑 停止服务: docker-compose -f docker/docker-compose.debug-automation.yml down"
else
    echo "❌ Debug MCP服务启动失败"
    echo "📝 查看日志:"
    docker-compose -f docker/docker-compose.debug-automation.yml logs
    exit 1
fi
