#!/bin/bash
# =============================================================================
# 本地开发 — 启动 Portal + 后端
# =============================================================================

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"

echo "=========================================="
echo "IT智能服务台 — 本地开发启动"
echo "=========================================="

# 1. 初始化角色数据（如果需要）
echo ""
echo ">>> 初始化角色数据..."
cd "$PROJECT_DIR/backend"
python scripts/init_roles.py

# 2. 启动后端（后台）
echo ""
echo ">>> 启动后端服务..."
cd "$PROJECT_DIR/backend"
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000 &
BACKEND_PID=$!
echo "后端 PID: $BACKEND_PID"

# 3. 启动 Portal 前端
echo ""
echo ">>> 启动 Portal 前端..."
cd "$PROJECT_DIR/frontend-portal"
npm run dev &
PORTAL_PID=$!
echo "Portal PID: $PORTAL_PID"

# 4. 启动 H5 前端
echo ""
echo ">>> 启动 H5 前端..."
cd "$PROJECT_DIR/frontend-h5"
npm run dev &
H5_PID=$!
echo "H5 PID: $H5_PID"

# 5. 启动 Agent 前端
echo ""
echo ">>> 启动 Agent 前端..."
cd "$PROJECT_DIR/frontend-agent"
npm run dev &
AGENT_PID=$!
echo "Agent PID: $AGENT_PID"

echo ""
echo "=========================================="
echo "所有服务已启动！"
echo "=========================================="
echo ""
echo "访问地址："
echo "  Portal: http://localhost:5176/itportal/"
echo "  H5 用户端: http://localhost:5174/itdesk/"
echo "  坐席工作台: http://localhost:5173/itagent/"
echo "  后端 API: http://localhost:8000/docs"
echo ""
echo "停止所有服务: Ctrl+C"
echo ""

# 等待所有后台进程
wait
