@echo off
REM ==========================================================
REM  MRI physics based augmentation - Windows one-click setup
REM  - Installs Python 3.12.10 via winget (if missing)
REM  - Creates venv .venv
REM  - Installs requirements
REM  - Runs Streamlit app
REM  No PowerShell required.
REM ==========================================================

SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

SET PY_REQ_VER=3.12
SET PY_FULL_VER=3.12.10
SET APP_PORT=8501

ECHO.
ECHO [INFO] Working directory: %CD%

REM --- Step 1: Check Python 3.12 availability ---
SET "PYCMD="
py -%PY_REQ_VER% -V >NUL 2>&1
IF %ERRORLEVEL%==0 (
  SET "PYCMD=py -%PY_REQ_VER%"
) ELSE (
  FOR /F "tokens=2 delims= " %%v IN ('python -V 2^>NUL') DO SET "PYV=%%v"
  ECHO !PYV! | FINDSTR /B "%PY_REQ_VER%" >NUL 2>&1
  IF !ERRORLEVEL!==0 SET "PYCMD=python"
)

IF "%PYCMD%"=="" (
  ECHO [WARN] Python %PY_REQ_VER% not found. Trying to install %PY_FULL_VER% via winget...
  winget --version >NUL 2>&1
  IF ERRORLEVEL 1 (
    ECHO [ERROR] winget is not available. Please install Python %PY_FULL_VER% from:
    ECHO         https://www.python.org/downloads/windows/
    ECHO         Then re-run this script.
    PAUSE
    EXIT /B 1
  )
  winget install -e --id Python.Python.3.12 --version %PY_FULL_VER% --scope user --silent
  IF ERRORLEVEL 1 (
    ECHO [ERROR] winget failed to install Python %PY_FULL_VER%. Install manually and re-run.
    PAUSE
    EXIT /B 1
  )
  REM Try again after install
  py -%PY_REQ_VER% -V >NUL 2>&1 && SET "PYCMD=py -%PY_REQ_VER%"
  IF "%PYCMD%"=="" (
    python -V 2>NUL | FIND "Python %PY_REQ_VER%" >NUL && SET "PYCMD=python"
  )
)

IF "%PYCMD%"=="" (
  ECHO [ERROR] Python %PY_REQ_VER% still not available in PATH. Close/reopen this window and run again.
  PAUSE
  EXIT /B 1
) ELSE (
  ECHO [INFO] Using Python command: %PYCMD%
)

REM --- Step 2: Create virtual environment ---
IF NOT EXIST .venv (
  ECHO [INFO] Creating virtual environment (.venv)
  %PYCMD% -m venv .venv
  IF ERRORLEVEL 1 (
    ECHO [ERROR] Failed to create virtual environment.
    PAUSE
    EXIT /B 1
  )
) ELSE (
  ECHO [INFO] Virtual environment already exists (.venv)
)

REM --- Step 3: Activate venv ---
IF NOT EXIST .venv\Scripts\activate.bat (
  ECHO [ERROR] Activation script not found: .venv\Scripts\activate.bat
  PAUSE
  EXIT /B 1
)
CALL .venv\Scripts\activate

REM --- Step 4: Ensure requirements.txt ---
IF NOT EXIST requirements.txt (
  ECHO [INFO] Creating requirements.txt
  > requirements.txt ECHO streamlit==1.37.0
  >>requirements.txt ECHO pillow>=10.3.0
  >>requirements.txt ECHO numpy>=1.26.4
  >>requirements.txt ECHO pydicom>=2.4.4
  >>requirements.txt ECHO nibabel>=5.2.1
)

REM --- Step 5: Upgrade pip and install deps ---
python -m pip install --upgrade pip
IF ERRORLEVEL 1 ECHO [WARN] pip upgrade failed, continuing...

pip install -r requirements.txt
IF ERRORLEVEL 1 (
  ECHO [ERROR] Failed to install dependencies from requirements.txt
  PAUSE
  EXIT /B 1
)

REM --- Step 6: Create assets folder (optional) ---
IF NOT EXIST assets (
  ECHO [INFO] Creating assets folder
  mkdir assets >NUL 2>&1
)

REM --- Step 7: Run the app ---
ECHO.
ECHO [INFO] Launching app on port %APP_PORT% ...
streamlit run app.py --server.port %APP_PORT% --server.address 127.0.0.1

ECHO.
ECHO [INFO] Streamlit exited. Press any key to close this window.
PAUSE
