@echo off
title PicoBot Gateway

:: Locate exe (prefer same-dir for distribution, fallback to target/ for dev)
set EXE=
if exist "%~dp0picobot.exe" (
    set EXE=%~dp0picobot.exe
) else if exist "%~dp0..\..\target\release\picobot.exe" (
    set EXE=%~dp0..\..\target\release\picobot.exe
) else if exist "%~dp0..\..\target\debug\picobot.exe" (
    set EXE=%~dp0..\..\target\debug\picobot.exe
) else (
    echo [ERROR] picobot.exe not found. Build first: cargo build --release
    pause
    exit /b 1
)

echo [INFO] Starting PicoBot Gateway in background...
echo [INFO] exe: %EXE%

:: /B = background, no new window
start "" /B "%EXE%" gateway

echo [INFO] Gateway started
echo [INFO] URL: http://127.0.0.1:19876
echo [INFO] WebSocket: ws://127.0.0.1:19876/ws

:: Wait a moment and check if still running
timeout /t 2 /nobreak >nul
tasklist /fi "imagename eq picobot.exe" 2>nul | find "picobot.exe" >nul
if %errorlevel% equ 0 (
    echo [OK] Gateway is running
) else (
    echo [WARN] Gateway may have failed to start, check logs
)

pause
