@echo off
chcp 65001 >nul 2>nul
setlocal enableextensions

rem ============================================================================
rem  Build platform frontend, sync to MiGu.Server\wwwroot, build MiGu.Server, run.
rem ----------------------------------------------------------------------------
rem  Usage:
rem    build-platform-and-run.bat [debug|release] [--reinstall] [--npm|--pnpm]
rem
rem  Examples:
rem    build-platform-and-run.bat
rem    build-platform-and-run.bat release
rem    build-platform-and-run.bat debug --reinstall
rem ============================================================================

set "ROOT=%~dp0"
set "FE_ROOT=%ROOT%frontends"
set "FE_APP=%FE_ROOT%\apps\simple-platform-vue"
set "FE_DIST=%FE_APP%\dist"
set "SERVER_ROOT=%ROOT%MiGu.Server"
set "SERVER_CSPROJ=%SERVER_ROOT%\MiGu.Server.csproj"
set "WWWROOT=%SERVER_ROOT%\wwwroot"

set "CONFIG=Debug"
set "NEED_INSTALL=0"
set "FORCE_PM="

:argloop
if "%~1"=="" goto argdone
if /I "%~1"=="debug" set "CONFIG=Debug"
if /I "%~1"=="release" set "CONFIG=Release"
if /I "%~1"=="-r" set "CONFIG=Release"
if /I "%~1"=="/r" set "CONFIG=Release"
if /I "%~1"=="--reinstall" set "NEED_INSTALL=1"
if /I "%~1"=="--npm" set "FORCE_PM=npm"
if /I "%~1"=="--pnpm" set "FORCE_PM=pnpm"
shift
goto argloop
:argdone

if not exist "%FE_ROOT%\package.json" (
  echo [ERROR] Missing frontend workspace: %FE_ROOT%\package.json
  exit /b 1
)

if not exist "%SERVER_CSPROJ%" (
  echo [ERROR] Missing server project: %SERVER_CSPROJ%
  exit /b 1
)

where dotnet >nul 2>nul
if errorlevel 1 (
  echo [ERROR] dotnet was not found. Install .NET 8 SDK first.
  exit /b 1
)

set "PM=%FORCE_PM%"
if "%PM%"=="" (
  where pnpm >nul 2>nul
  if not errorlevel 1 set "PM=pnpm"
)
if "%PM%"=="" set "PM=npm"

if /I "%PM%"=="pnpm" (
  where pnpm >nul 2>nul
) else (
  where npm >nul 2>nul
)
if errorlevel 1 (
  echo [ERROR] Package manager not found: %PM%
  exit /b 1
)

echo ============================================================
echo  Migu2.0 build and run
echo    frontend : %FE_APP%
echo    wwwroot  : %WWWROOT%
echo    server   : %SERVER_CSPROJ%
echo    config   : %CONFIG%
echo    pm       : %PM%
echo ============================================================
echo.

pushd "%FE_ROOT%"
if errorlevel 1 exit /b 1

if not exist "node_modules" set "NEED_INSTALL=1"
if "%NEED_INSTALL%"=="1" (
  echo === [1/5] %PM% install ===
  call %PM% install
  if errorlevel 1 (
    popd
    echo [ERROR] Frontend dependencies install failed.
    exit /b 1
  )
) else (
  echo === [1/5] dependencies already installed, skip ===
)

echo.
echo === [2/5] build frontend ===
if /I "%PM%"=="pnpm" (
  call pnpm --filter simple-platform-vue build
) else (
  pushd "%FE_APP%"
  call npm run build
  popd
)
if errorlevel 1 (
  popd
  echo [ERROR] Frontend build failed.
  exit /b 1
)
popd

if not exist "%FE_DIST%\index.html" (
  echo [ERROR] Frontend dist was not generated: %FE_DIST%
  exit /b 1
)

echo.
echo === [3/5] sync dist to MiGu.Server\wwwroot ===
if not exist "%WWWROOT%" mkdir "%WWWROOT%"
robocopy "%FE_DIST%" "%WWWROOT%" /MIR /NFL /NDL /NJH /NJS /NP
set "ROBOCOPY_EC=%ERRORLEVEL%"
if %ROBOCOPY_EC% GEQ 8 (
  echo [ERROR] robocopy failed, errorlevel=%ROBOCOPY_EC%
  exit /b %ROBOCOPY_EC%
)

echo.
echo === [4/5] build MiGu.Server ===
pushd "%SERVER_ROOT%"
if errorlevel 1 exit /b 1
dotnet build "MiGu.Server.csproj" -c %CONFIG% --nologo
if errorlevel 1 (
  popd
  echo [ERROR] MiGu.Server build failed.
  exit /b 1
)

echo.
echo === [5/5] run MiGu.Server ===
echo [INFO] Press Ctrl+C to stop. Open http://localhost:8080/
echo.
dotnet run --project "MiGu.Server.csproj" -c %CONFIG% --no-build --no-restore
set "RUN_EC=%ERRORLEVEL%"
popd

endlocal & exit /b %RUN_EC%
