@echo off
chcp 65001 >nul
setlocal ENABLEDELAYEDEXPANSION

REM ==========================================================
REM  MiGu.Server build and run (one-click)
REM   - default Debug; pass "release" / "-r" / "/r" for Release
REM   - listens on http://0.0.0.0:8080 (see Properties\launchSettings.json)
REM   - YARP proxies:  /api/sl/* -> :8222 ;  /vr/* -> :8223
REM ==========================================================

cd /d "%~dp0"

set "CONFIG=Debug"
if /I "%~1"=="release" set "CONFIG=Release"
if /I "%~1"=="-r"      set "CONFIG=Release"
if /I "%~1"=="/r"      set "CONFIG=Release"

echo ============================================================
echo   MiGu.Server 生成 + 启动  ( Config = %CONFIG% )
echo   目录: %CD%
echo ============================================================
echo.

where dotnet >nul 2>&1
if errorlevel 1 (
    echo [错误] 未找到 dotnet ^( .NET 8 SDK ^)。请先安装 .NET 8 SDK 并重新打开终端。
    pause
    exit /b 1
)

echo === [1/2] dotnet build ===
dotnet build "MiGu.Server.csproj" -c %CONFIG% --nologo
if errorlevel 1 (
    echo.
    echo *** 构建失败，已停止。请检查上方编译错误。 ***
    pause
    exit /b 1
)

echo.
echo === [2/2] dotnet run ===
echo [提示] Ctrl+C 终止；浏览器访问 http://localhost:8080/  ^(Swagger: /swagger^)
echo.

dotnet run --project "MiGu.Server.csproj" -c %CONFIG% --no-build --no-restore
set "RUN_EC=%errorlevel%"

echo.
if not "%RUN_EC%"=="0" (
    echo *** MiGu.Server 退出，errorlevel=%RUN_EC%
) else (
    echo MiGu.Server 正常退出。
)
endlocal & exit /b %RUN_EC%
