@echo off
setlocal
chcp 65001 >nul
cls

for %%I in ("%~dp0.") do set "REPO_DIR=%%~fI"
pushd "%REPO_DIR%"

set "YOLO_MODE="
where yolo >nul 2>nul
if %ERRORLEVEL% equ 0 goto launcher_cli

py -3.11 -c "import ultralytics" >nul 2>nul
if %ERRORLEVEL% equ 0 goto launcher_py

python -c "import ultralytics" >nul 2>nul
if %ERRORLEVEL% equ 0 goto launcher_python

echo [ERROR] No usable Ultralytics launcher was found.
echo.
echo Make sure one of the following works:
echo   1. yolo is in PATH
echo   2. py -3.11 can import ultralytics
echo   3. python can import ultralytics
goto fail

:launcher_cli
set "YOLO_MODE=cli"
goto launcher_done

:launcher_py
set "YOLO_MODE=py"
goto launcher_done

:launcher_python
set "YOLO_MODE=python"
goto launcher_done

:launcher_done
set "DATASET=%REPO_DIR%\datasets\shoe-roi-mix\data.yaml"
set "MODEL=yolov8s.pt"
set "EPOCHS=150"
set "IMGSZ=640"
set "BATCH=16"
set "PROJECT=%REPO_DIR%\runs\roi_yolov8s_640"
set "RUN_NAME=train_roi"

if defined DATASET_OVERRIDE set "DATASET=%DATASET_OVERRIDE%"
if defined MODEL_OVERRIDE set "MODEL=%MODEL_OVERRIDE%"
if defined EPOCHS_OVERRIDE set "EPOCHS=%EPOCHS_OVERRIDE%"
if defined IMGSZ_OVERRIDE set "IMGSZ=%IMGSZ_OVERRIDE%"
if defined BATCH_OVERRIDE set "BATCH=%BATCH_OVERRIDE%"
if defined PROJECT_OVERRIDE set "PROJECT=%PROJECT_OVERRIDE%"
if defined RUN_NAME_OVERRIDE set "RUN_NAME=%RUN_NAME_OVERRIDE%"

if not exist "%DATASET%" (
    echo [ERROR] ROI dataset yaml not found: %DATASET%
    echo.
    echo Build the ROI dataset first:
    echo   python "%REPO_DIR%\09_build_roi_shoe_dataset.py" --clean
    goto fail
)

echo ============================================================
echo      Train ROI Shoe Detector ^(YOLOv8s + 640x640^)
echo ============================================================
echo.
echo [INFO] Repo: %REPO_DIR%
echo [INFO] Dataset: %DATASET%
echo [INFO] Model: %MODEL%
echo [INFO] Launcher: %YOLO_MODE%
echo.
echo Training params:
echo   Epochs: %EPOCHS%
echo   Image Size: %IMGSZ%x%IMGSZ%
echo   Batch Size: %BATCH%
echo   Device: GPU ^(cuda:0^)
echo   Project: %PROJECT%
echo   Run Name: %RUN_NAME%
echo.

if defined DRY_RUN (
    echo [INFO] DRY_RUN is set. Exiting before training.
    goto success
)

if "%YOLO_MODE%"=="cli" goto run_cli
if "%YOLO_MODE%"=="py" goto run_py
if "%YOLO_MODE%"=="python" goto run_python

echo [ERROR] Internal launcher selection error.
goto fail

:run_cli
yolo detect train data="%DATASET%" model="%MODEL%" epochs=%EPOCHS% imgsz=%IMGSZ% batch=%BATCH% device=0 project="%PROJECT%" name="%RUN_NAME%"
goto check_result

:run_py
py -3.11 -m ultralytics detect train data="%DATASET%" model="%MODEL%" epochs=%EPOCHS% imgsz=%IMGSZ% batch=%BATCH% device=0 project="%PROJECT%" name="%RUN_NAME%"
goto check_result

:run_python
python -m ultralytics detect train data="%DATASET%" model="%MODEL%" epochs=%EPOCHS% imgsz=%IMGSZ% batch=%BATCH% device=0 project="%PROJECT%" name="%RUN_NAME%"
goto check_result

:check_result
if %ERRORLEVEL% neq 0 (
    echo.
    echo [ERROR] Training failed.
    goto fail
)

:success
echo.
echo ============================================================
echo                     Training Complete
echo ============================================================
echo.
echo Output project: %PROJECT%
echo.
popd
endlocal
exit /b 0

:fail
echo.
popd
pause
endlocal
exit /b 1
