@echo off
setlocal
cd /d "%~dp0"

echo.
echo ============================================================
echo   ONEBOT one-click start: web + GenServer + NX worker
echo ============================================================
echo.

echo [1/6] stop old processes ...
taskkill /F /IM GenServer.exe >nul 2>nul
taskkill /F /IM run_journal.exe >nul 2>nul
ping -n 2 127.0.0.1 >nul

echo [2/6] build GenServer ...
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0tools\build-genserver.ps1"
if not %errorlevel%==0 (
  echo.
  echo [FAIL] GenServer build failed. Check error above.
  pause
  exit /b 1
)

echo [3/6] start GenServer ...
start "GenServer" /min "%~dp0bin\GenServer.exe" "%~dp0genserver-local\genserver-config.json"
ping -n 3 127.0.0.1 >nul

echo [4/6] start NX worker ...
start "NXworker" /min "C:\Program Files\Siemens\NX2506\NXBIN\run_journal.exe" "%~dp0sample-data\tools\worker_journal.vb"
ping -n 3 127.0.0.1 >nul

echo [5/6] start web server (port 8080) ...
start "OnebotWeb" /min powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0tools\serve-web.ps1"
ping -n 4 127.0.0.1 >nul

echo [6/6] health check ...
curl.exe -s http://localhost:8899/health
echo.
curl.exe -s -o nul -w "web 8080 : %%{http_code}" http://localhost:8080/index.html
echo.

echo.
echo ============================================================
echo   done. Open browser:  http://localhost:8080
echo   admin: http://localhost:8899/admin  (password: onebot888)
echo   keep the GenServer / NXworker / OnebotWeb windows open
echo ============================================================
echo.
pause
endlocal
