@echo off
setlocal EnableExtensions
cd /d "%~dp0"
title GAL-FINGERPRINT-I Chamber v0.2.2 — Gate C

rem Keep the environment and active checkpoints outside deeply nested project paths.
set "GFI_ENV_ROOT=%LOCALAPPDATA%\UNNS\GFI022_PY"
set "GFI_PY=%GFI_ENV_ROOT%\Scripts\python.exe"
set "GFI_RUNTIME_ROOT=%LOCALAPPDATA%\UNNS\GFI022_RUNTIME"
set "PYLAUNCHER=python"
where py >nul 2>nul && set "PYLAUNCHER=py -3"

if not exist "%GFI_PY%" (
  echo Creating short-path Python environment:
  echo   %GFI_ENV_ROOT%
  if not exist "%LOCALAPPDATA%\UNNS" mkdir "%LOCALAPPDATA%\UNNS"
  %PYLAUNCHER% -m venv "%GFI_ENV_ROOT%" || goto :error
)

"%GFI_PY%" -c "import fastapi,uvicorn,sklearn,pandas,numpy" >nul 2>nul
if errorlevel 1 (
  echo Installing chamber requirements into the short-path environment...
  "%GFI_PY%" -m pip install --upgrade pip setuptools wheel || goto :error
  "%GFI_PY%" -m pip install --prefer-binary -r "%~dp0requirements.txt" || goto :error
)

set "PYTHONPATH=%~dp0;%PYTHONPATH%"
set "PYTHONUNBUFFERED=1"

echo Running Gate C startup preflight...
"%GFI_PY%" -m gal_fingerprint_i.preflight || goto :error

echo Starting GAL-FINGERPRINT-I Chamber v0.2.2 — Gate C...
"%GFI_PY%" -m gal_fingerprint_i.web --open-browser || goto :error
exit /b 0

:error
echo.
echo GAL-FINGERPRINT-I Gate C chamber could not start. The error above is the controlling diagnostic.
echo Environment location:
echo   %GFI_ENV_ROOT%
echo Chamber location:
echo   %~dp0
pause
exit /b 1
