@echo off
echo ========================================
echo   AI Clinical Research Platform
echo   One-click Startup Script
echo ========================================
echo.

echo [1/7] Checking Docker status...
docker ps >nul 2>&1
if errorlevel 1 (
    echo ERROR: Docker is not running. Please start Docker Desktop first.
    pause
    exit /b 1
)
echo OK: Docker is running

echo [2/7] Starting PostgreSQL and Redis containers...
cd /d "%~dp0"
docker-compose up -d
if errorlevel 1 (
    echo ERROR: Failed to start Docker containers
    pause
    exit /b 1
)
echo OK: Database containers started

echo [3/7] Waiting for database to be ready...
timeout /t 5 /nobreak >nul
echo OK: Database is ready

echo [4/7] Starting Python extraction service (new window)...
start "Document Extraction Service" cmd /k "cd /d %~dp0extraction_service && venv\Scripts\python.exe -m uvicorn main:app --host 0.0.0.0 --port 8000"
echo OK: Python service starting...

echo [5/7] Waiting for extraction service...
timeout /t 3 /nobreak >nul

echo [6/7] Starting backend server (new window)...
start "AI Clinical Research - Backend" cmd /k "cd /d %~dp0backend && npm run dev"
echo OK: Backend starting...

echo [7/7] Waiting for backend, then starting frontend...
timeout /t 3 /nobreak >nul

start "AI Clinical Research - Frontend" cmd /k "cd /d %~dp0frontend && npm run dev"
echo OK: Frontend starting...

echo.
echo ========================================
echo   Startup Complete!
echo ========================================
echo.
echo Python Service: http://localhost:8000 (Docs: /docs)
echo Backend:        http://localhost:3001
echo Frontend:       http://localhost:5173
echo.
echo Please wait 10 seconds before accessing the frontend...
echo.
timeout /t 10 /nobreak
start http://localhost:5173
echo.
echo Press any key to close this window...
pause >nul
