@echo off
REM ZP2 Startup Script for Windows / ZP2 Windows 启动脚本

setlocal EnableDelayedExpansion

set "SCRIPT_DIR=%~dp0"
set "PROJECT_DIR=%SCRIPT_DIR%.."
set "BACKEND_DIR=%PROJECT_DIR%\backend"
set "FRONTEND_DIR=%PROJECT_DIR%\frontend"

echo ================================
echo   ZP2 Game Server Launcher
echo   ZP2 游戏服务器启动器
echo ================================
echo.

REM Check prerequisites / 检查依赖
echo [INFO] Checking prerequisites / 检查依赖...

where java >nul 2>nul
if %ERRORLEVEL% neq 0 (
    echo [ERROR] Java not found. Please install Java 17+. / 未找到 Java，请安装 Java 17+。
    exit /b 1
)

for /f "tokens=3" %%g in ('java -version 2^>^1 ^| findstr /i "version"') do (
    set "JAVA_VER=%%g"
    set "JAVA_VER=!JAVA_VER:"=!"
)
echo [INFO] Java !JAVA_VER! found / 找到 Java !JAVA_VER!

where mvn >nul 2>nul
if %ERRORLEVEL% neq 0 (
    echo [ERROR] Maven not found. Please install Maven. / 未找到 Maven，请安装 Maven。
    exit /b 1
)
echo [INFO] Maven found / 找到 Maven

where node >nul 2>nul
if %ERRORLEVEL% neq 0 (
    echo [ERROR] Node.js not found. Please install Node.js. / 未找到 Node.js，请安装 Node.js。
    exit /b 1
)
for /f "tokens=*" %%v in ('node -v') do echo [INFO] %%v found / 找到 %%v

echo.

REM Build backend / 构建后端
echo [INFO] Building backend / 构建后端...
cd /d "%BACKEND_DIR%"
call mvn package -q
if %ERRORLEVEL% neq 0 (
    echo [ERROR] Backend build failed / 后端构建失败
    exit /b 1
)
echo [INFO] Backend built successfully / 后端构建成功
echo.

REM Start backend / 启动后端
echo [INFO] Starting backend server on port 8080 / 启动后端服务器 (端口 8080)...
cd /d "%PROJECT_DIR%"
start "ZP2 Backend" java -jar backend\target\zombie-crisis-server-1.0.0.jar
echo [INFO] Backend started in new window / 后端已在新窗口中启动
echo.

REM Start frontend / 启动前端
echo [INFO] Starting frontend dev server / 启动前端开发服务器...
cd /d "%FRONTEND_DIR%"

if not exist "node_modules\" (
    echo [INFO] Installing frontend dependencies / 安装前端依赖...
    call npm install
)

echo [INFO] Frontend dev server starting at http://localhost:3000 / 前端开发服务器地址: http://localhost:3000
echo.
echo Press Ctrl+C to stop the frontend. Backend runs in a separate window.
echo 按 Ctrl+C 停止前端，后端运行在独立窗口中。
echo.

call npm run dev
