@echo off
REM MHA Flow v3.0 - Production Launch Script for Windows

echo ================================================================================
echo MHA FLOW v3.0 - PRODUCTION SERVER LAUNCHER
echo ================================================================================
echo.

REM Activate virtual environment if it exists
if exist ".venv\Scripts\activate.bat" (
    echo Activating virtual environment...
    call .venv\Scripts\activate.bat
    echo Virtual environment activated
    echo.
)

REM Check Python installation
python --version >nul 2>&1
if %errorlevel% neq 0 (
    echo ERROR: Python not found. Please install Python 3.8 or higher.
    pause
    exit /b 1
)

echo [1/5] Checking Python installation...
python --version
echo.

REM Install/upgrade dependencies
echo [2/5] Installing dependencies...
pip install -r requirements_production.txt --quiet
if %errorlevel% neq 0 (
    echo ERROR: Failed to install dependencies
    pause
    exit /b 1
)
echo Dependencies installed successfully
echo.

REM Initialize database
echo [3/5] Initializing database...
if not exist "mha_production.db" (
    echo Setting up database for the first time...
    python init_db.py full
    if %errorlevel% neq 0 (
        echo ERROR: Database initialization failed
        pause
        exit /b 1
    )
) else (
    echo Database already exists
)
echo.

REM Check algorithms
echo [4/5] Validating algorithms...
python -c "from mha_toolbox.complete_algorithm_registry_v2 import AlgorithmRegistry; r = AlgorithmRegistry(); print(f'Available algorithms: {r.get_total_count()}')"
if %errorlevel% neq 0 (
    echo Warning: Algorithm validation encountered issues
)
echo.

REM Start server
echo [5/5] Starting Flask server...
echo.
echo ================================================================================
echo SERVER INFORMATION
echo ================================================================================
echo URL: http://localhost:5000
echo Admin: http://localhost:5000/login
echo API Docs: http://localhost:5000/api
echo.
echo Default Accounts:
echo   Admin - Username: admin, Password: admin123
echo   Demo  - Username: demo,  Password: demo123
echo.
echo Press Ctrl+C to stop the server
echo ================================================================================
echo.

python app_production.py

pause
