@echo off
setlocal enabledelayedexpansion

echo ========================================
echo ML Model Docker Runner
echo ========================================

REM Check if MySQL is running
echo Checking MySQL service...
sc query mysql >nul 2>&1
if %errorlevel% equ 0 (
    net start mysql >nul 2>&1
    echo MySQL service is running.
) else (
    echo WARNING: MySQL service not found or not running!
    echo Please ensure MySQL is installed and running before starting the container.
    echo You can run 'start_mysql.bat' to attempt to start MySQL.
    echo.
    set /p continue="Continue anyway? (y/n): "
    if /i not "!continue!"=="y" (
        echo Aborting...
        pause
        exit /b 1
    )
)

REM Stop and remove existing container if running
echo Stopping any existing containers...
docker stop ml-model-container 2>nul
docker rm ml-model-container 2>nul

REM Remove existing image to ensure fresh build
echo Removing existing image...
docker rmi ml-model 2>nul

REM Build the Docker image
echo Building Docker image...
docker build -t ml-model .
if %errorlevel% neq 0 (
    echo ERROR: Docker build failed!
    pause
    exit /b 1
)

echo.
echo ========================================
echo Build successful! Starting container...
echo ========================================

REM Run the container with host networking to access localhost MySQL
echo Running container with host networking...
echo Note: The container will try to connect to MySQL on localhost:3306
echo.
docker run --name ml-model-container --network host -e DB_HOST=localhost -e DB_PASS=4429@Root ml-model

REM Alternative: If you want to use bridge networking, uncomment this line:
REM docker run --name ml-model-container -p 6546:6546 -e DB_HOST=host.docker.internal -e DB_PASS=4429@Root ml-model

echo.
echo Container stopped. Press any key to exit...
pause 