#!/bin/bash
# A2A 서버들만 실행하는 스크립트

echo "=== A2A 서버 시작 ==="
echo ""

# 환경 변수 로드
if [ -f .env ]; then
    export $(cat .env | grep -v '^#' | xargs)
fi

# 포트 설정
export PLANNING_AGENT_PORT=8003
export RESEARCH_AGENT_PORT=8001
export REPORT_WRITING_AGENT_PORT=8004
export MCP_SERVER_PORT=8090

# 프로세스 종료 함수
cleanup() {
    echo "\n=== 서버 종료 중... ==="
    kill $(jobs -p) 2>/dev/null
    exit 0
}

# 시그널 핸들러 등록
trap cleanup SIGINT SIGTERM

# 1. MCP 서버 시작
echo "1. MCP 서버 시작 (포트 $MCP_SERVER_PORT)..."
python all-search-mcp/run_server.py --transport http --port $MCP_SERVER_PORT &
sleep 3

# 2. Planning Agent A2A 서버
echo "\n2. Planning Agent A2A 서버 시작 (포트 $PLANNING_AGENT_PORT)..."
python agents/a2a_servers/planning_a2a_server.py &
sleep 2

# 3. Research Agent A2A 서버
echo "\n3. Research Agent A2A 서버 시작 (포트 $RESEARCH_AGENT_PORT)..."
python agents/a2a_servers/research_a2a_server.py &
sleep 2

# 4. Report Writing Agent A2A 서버
echo "\n4. Report Writing Agent A2A 서버 시작 (포트 $REPORT_WRITING_AGENT_PORT)..."
python agents/a2a_servers/report_writing_a2a_server.py &
sleep 2

echo "\n=== 모든 A2A 서버가 시작되었습니다 ==="
echo ""
echo "접속 URL:"
echo "- Planning Agent: http://localhost:$PLANNING_AGENT_PORT"
echo "- Research Agent: http://localhost:$RESEARCH_AGENT_PORT"
echo "- Report Writing Agent: http://localhost:$REPORT_WRITING_AGENT_PORT"
echo "- MCP Server: http://localhost:$MCP_SERVER_PORT/mcp"
echo ""
echo "AgentCard 확인:"
echo "- http://localhost:$PLANNING_AGENT_PORT/v1/card"
echo "- http://localhost:$RESEARCH_AGENT_PORT/v1/card"
echo "- http://localhost:$REPORT_WRITING_AGENT_PORT/v1/card"
echo ""
echo "종료하려면 Ctrl+C를 누르세요."

# 백그라운드 프로세스 대기
wait