@echo off
REM Quick Database Deployment Script for Windows

echo ==========================================
echo DATABASE DEPLOYMENT SCRIPT
echo ==========================================

REM Check if virtual environment is activated
if "%VIRTUAL_ENV%"=="" (
    echo Warning: Virtual environment not activated
    echo Attempting to activate...
    if exist venv\Scripts\activate.bat (
        call venv\Scripts\activate.bat
    ) else if exist env\Scripts\activate.bat (
        call env\Scripts\activate.bat
    )
)

REM Step 1: Check database connection
echo.
echo === Checking Database Connection ===
python -c "import django; django.setup(); from django.db import connection; connection.ensure_connection(); print('Connected')" 2>nul
if errorlevel 1 (
    echo Database connection failed
    exit /b 1
)

REM Step 2: Show pending migrations
echo.
echo === Checking Pending Migrations ===
python manage.py showmigrations --plan | findstr "[ ]"

REM Step 3: Apply migrations
echo.
echo === Applying Migrations ===
python manage.py migrate --noinput
if errorlevel 1 (
    echo Migration failed
    exit /b 1
)

REM Step 4: Collect static files
echo.
echo === Collecting Static Files ===
python manage.py collectstatic --noinput --clear

REM Step 5: Run system checks
echo.
echo === Running System Checks ===
python manage.py check

echo.
echo ==========================================
echo DEPLOYMENT COMPLETED
echo ==========================================
