@echo off
chcp 65001 >nul
setlocal EnableExtensions
title Server Manager

rem Resolve repository root from scripts\run\.
for %%I in ("%~dp0..\..") do set "ROOT=%%~fI"

rem SERVER_MANAGER_ROOT can override the default managed server root.
set "SERVER_ROOT=%ROOT%"
if defined SERVER_MANAGER_ROOT set "SERVER_ROOT=%SERVER_MANAGER_ROOT%"

rem Run source modules through the new entrypoints package.
cd /d "%ROOT%"
set "PYTHONPATH=%ROOT%\src;%PYTHONPATH%"

rem Prefer packaged ServerManager.exe when present.
set "SM_MAIN="
if defined SERVER_MANAGER_MAIN if exist "%SERVER_MANAGER_MAIN%" set "SM_MAIN=%SERVER_MANAGER_MAIN%"
if not defined SM_MAIN if exist "%ROOT%\src\dist\ServerManager\ServerManager.exe" set "SM_MAIN=%ROOT%\src\dist\ServerManager\ServerManager.exe"

rem No arguments enters interactive CLI mode.
if "%~1"=="" goto :run_interactive

rem Command dispatch.
if /i "%~1"=="help" goto :show_help
if /i "%~1"=="--help" goto :show_help
if /i "%~1"=="-h" goto :show_help
if /i "%~1"=="gui" goto :run_gui
if /i "%~1"=="mcp" goto :run_mcp

rem Pass commands to CLI entrypoint.
echo [INFO] 服务器根目录: %SERVER_ROOT%
if defined SM_MAIN (
    echo [INFO] 使用打包程序: %SM_MAIN%
    "%SM_MAIN%" --root "%SERVER_ROOT%" %*
    goto :end
)
python --version >nul 2>&1
if errorlevel 1 (
    echo [错误] 未找到 Python，请先安装 Python 3.8+
    pause
    exit /b 1
)
python -m entrypoints.cli --root "%SERVER_ROOT%" %*
goto :end

:run_interactive
echo [INFO] 服务器根目录: %SERVER_ROOT%
if defined SM_MAIN (
    echo [INFO] 使用打包程序: %SM_MAIN%
    "%SM_MAIN%" --root "%SERVER_ROOT%" interactive
    goto :end
)
python --version >nul 2>&1
if errorlevel 1 (
    echo [错误] 未找到 Python，请先安装 Python 3.8+
    pause
    exit /b 1
)
python -m entrypoints.cli --root "%SERVER_ROOT%" interactive
goto :end

:show_help
echo Server Manager - Windows
echo.
echo Usage: %~nx0 [command] [args]
echo.
echo Server root: %SERVER_ROOT%
echo.
echo Commands:
echo   list
echo   create
echo   start
echo   stop
echo   connect
echo   compile
echo   regen
echo   info
echo   status
echo   config
echo   logs
echo   command
echo   ids
echo   doctor
echo   migrate
echo   mcp
echo   gui
echo.
echo Examples:
echo   %~nx0 list
echo   %~nx0 create --type game --id 1
echo   %~nx0 start --server ddxq_game_s1 --background
echo   %~nx0 compile --target proto
echo   %~nx0 status --json
echo   %~nx0 config show --server ddxq_game_s1
echo   %~nx0 logs search ddxq_game_s1 error
echo   %~nx0 command start ddxq_game_s1
echo   %~nx0 mcp
echo   %~nx0 gui
echo.
echo More help: python -m entrypoints.cli --help
if defined SM_MAIN (
    echo.
    echo Packaged program: %SM_MAIN%
)
goto :end

:run_gui
echo [INFO] 服务器根目录: %SERVER_ROOT%
if defined SM_MAIN (
    echo [INFO] 启动 GUI（打包程序）...
    "%SM_MAIN%"
    goto :end
)
python --version >nul 2>&1
if errorlevel 1 (
    echo [错误] 未找到 Python，请先安装 Python 3.8+
    pause
    exit /b 1
)
rem Check PyQt6 for source GUI mode.
python -c "import PyQt6" >nul 2>&1
if errorlevel 1 (
    echo [提示] 正在安装 PyQt6...
    pip install PyQt6
)
echo [INFO] 启动 GUI 界面...
python -m entrypoints
goto :end

:run_mcp
if defined SM_MAIN (
    "%SM_MAIN%" --root "%SERVER_ROOT%" mcp
    goto :end
)
python --version >nul 2>&1
if errorlevel 1 (
    echo [ERROR] Python not found. Install Python 3.8+
    exit /b 1
)
python -m entrypoints.cli --root "%SERVER_ROOT%" mcp
goto :end

:end
endlocal
exit /b 0
