@echo off
REM AutoBackups Playwright Test Runner Script for Windows
REM This script sets up the environment and runs Playwright tests

echo 🚀 AutoBackups - Running Playwright E2E Tests
echo =============================================

REM Check if conda is available
where conda >nul 2>nul
if %ERRORLEVEL% NEQ 0 (
    echo ❌ Conda is not installed or not in PATH
    echo Please install Miniconda/Anaconda and ensure it's in your PATH
    exit /b 1
)

REM Activate conda environment
echo 📦 Activating conda environment 'autobackups'...
call conda activate autobackups
if %ERRORLEVEL% NEQ 0 (
    echo ❌ Failed to activate conda environment 'autobackups'
    echo Please create the environment first:
    echo conda env create -f environment.yml
    exit /b 1
)

REM Check if Python dependencies are installed
echo 🔍 Checking Python dependencies...
python -c "import flask" >nul 2>nul
if %ERRORLEVEL% NEQ 0 (
    echo 📥 Installing Python dependencies...
    pip install -r requirements.txt
)

REM Check if Node.js dependencies are installed
if not exist "node_modules" (
    echo 📥 Installing Node.js dependencies...
    npm install
)

REM Install Playwright browsers if needed
if not exist "node_modules\@playwright" (
    echo 🌐 Installing Playwright browsers...
    npx playwright install
)

REM Create test results directory
if not exist "test-results" mkdir test-results

REM Run tests based on arguments
if "%1"=="headed" (
    echo 🎭 Running tests in headed mode...
    npx playwright test --headed
) else if "%1"=="debug" (
    echo 🐛 Running tests in debug mode...
    npx playwright test --debug
) else if "%1"=="specific" (
    if "%2"=="" (
        echo ❌ Please specify a test file
        echo Usage: %0 specific tests/e2e/dashboard.spec.js
        exit /b 1
    )
    echo 🎯 Running specific test: %2
    npx playwright test "%2"
) else if "%1"=="report" (
    echo 📊 Opening test report...
    npx playwright show-report
) else (
    echo 🏃 Running all tests...
    npx playwright test
)

if %ERRORLEVEL% EQU 0 (
    echo ✅ All tests passed!
) else (
    echo ❌ Some tests failed. Check the results above.
)

echo 📊 To view the detailed report, run: npx playwright show-report
echo 🔍 Test artifacts are saved in: test-results/

exit /b %ERRORLEVEL%
