#!/bin/bash

# 统一启动脚本 - 启动前后端服务

# 获取脚本所在目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BACKEND_DIR="$SCRIPT_DIR/lesingle-edu-reading-platform-backend"
FRONTEND_DIR="$SCRIPT_DIR/lesingle-edu-reading-platform-frontend"

echo "======================================"
echo "  少儿智慧阅读"
echo "  统一启动脚本"
echo "======================================"
echo ""

# 函数：检查端口是否被占用
check_port() {
    local port=$1
    local name=$2
    if lsof -ti:$port > /dev/null 2>&1; then
        echo "⚠️  端口 $port 已被占用 ($name)"
        read -p "是否停止并重启？(y/n) " -n 1 -r
        echo
        if [[ $REPLY =~ ^[Yy]$ ]]; then
            lsof -ti:$port | xargs kill -9 2>/dev/null
            echo "✅ 已停止占用端口 $port 的进程"
            return 0
        else
            return 1
        fi
    fi
    return 0
}

# 检查并停止占用端口的进程
check_port 8481 "后端"
if [ $? -eq 1 ]; then
    echo "❌ 后端启动取消"
    exit 1
fi

check_port 5174 "前端"
if [ $? -eq 1 ]; then
    echo "❌ 前端启动取消"
    exit 1
fi

# 启动后端
echo ""
echo "📦 [1/2] 启动后端服务 (Spring Boot)..."
cd "$BACKEND_DIR" || exit 1
echo "📂 后端目录：$(pwd)"

# 检查 JAVA_HOME 是否设置
if [ -z "$JAVA_HOME" ]; then
    echo "⚠️  未设置 JAVA_HOME，尝试使用默认 JDK 17 路径..."
    if [ -d "/f/Java/jdk-17" ]; then
        export JAVA_HOME="/f/Java/jdk-17"
        echo "✅ 设置 JAVA_HOME=$JAVA_HOME"
    else
        echo "⚠️  请确保已安装 JDK 17 并设置 JAVA_HOME"
    fi
fi

echo "☕ Java 版本：$(java -version 2>&1 | head -n 1)"

# 后台启动后端 (Maven)
nohup mvn spring-boot:run -Dspring-boot.run.forked > /tmp/lesingle-edu-reading-platform-backend.log 2>&1 &
BACKEND_PID=$!
echo "✅ 后端启动成功 (PID: $BACKEND_PID)"
echo "📄 日志文件：/tmp/lesingle-edu-reading-platform-backend.log"

# 等待后端启动
echo "⏳ 等待后端服务启动..."
for i in {1..60}; do
    if curl -s http://localhost:8080/actuator/health > /dev/null 2>&1; then
        echo "✅ 后端服务就绪"
        break
    fi
    if [ $i -eq 60 ]; then
        echo "❌ 后端服务启动超时"
        echo "📄 请查看日志：tail -f /tmp/lesingle-edu-reading-platform-backend.log"
        exit 1
    fi
    sleep 2
done

# 启动前端
echo ""
echo "📦 [2/2] 启动前端服务..."
cd "$FRONTEND_DIR" || exit 1
echo "📂 前端目录：$(pwd)"

if [ ! -d "node_modules" ]; then
    echo "📦 正在安装前端依赖..."
    npm install
fi

# 后台启动前端
nohup npm run dev > /tmp/lesingle-edu-reading-platform-frontend.log 2>&1 &
FRONTEND_PID=$!
echo "✅ 前端启动成功 (PID: $FRONTEND_PID)"
echo "📄 日志文件：/tmp/lesingle-edu-reading-platform-frontend.log"

# 等待前端启动
echo "⏳ 等待前端服务启动..."
for i in {1..30}; do
    if curl -s http://localhost:5173 > /dev/null 2>&1; then
        echo "✅ 前端服务就绪"
        break
    fi
    if [ $i -eq 30 ]; then
        echo "⚠️  前端服务可能需要更多时间启动"
        echo "📄 请查看日志：tail -f /tmp/lesingle-edu-reading-platform-frontend.log"
        break
    fi
    sleep 1
done

echo ""
echo "======================================"
echo "  ✅ 所有服务启动成功！"
echo "======================================"
echo ""
echo "📍 后端 API:     http://localhost:8080"
echo "📍 前端页面：    http://localhost:5173"
echo "📍 API 文档：   http://localhost:8080/api/v1"
echo ""
echo "📊 查看后端日志：tail -f /tmp/lesingle-edu-reading-platform-backend.log"
echo "📊 查看前端日志：tail -f /tmp/lesingle-edu-reading-platform-frontend.log"
echo ""
echo "🛑 停止所有服务：pkill -f 'spring-boot:run' && pkill -f 'vite'"
echo ""
