@echo off
REM Expenses Management System - Windows Deployment Script
REM HAVEN GRAZURI Advance

echo.
echo ========================================
echo EXPENSES SYSTEM DEPLOYMENT
echo ========================================
echo.
echo Started at: %date% %time%
echo.

REM Step 1: Check Python
echo [INFO] Checking Python installation...
python --version >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] Python not found!
    exit /b 1
)
echo [SUCCESS] Python found
echo.

REM Step 2: Check Django project
echo [INFO] Checking Django project...
if not exist "manage.py" (
    echo [ERROR] manage.py not found! Are you in the project root?
    exit /b 1
)
echo [SUCCESS] Django project found
echo.

REM Step 3: Install dependencies
echo ========================================
echo CHECKING DEPENDENCIES
echo ========================================
echo.
if exist "requirements.txt" (
    echo [INFO] Installing/updating dependencies...
    python -m pip install -r requirements.txt --quiet
    echo [SUCCESS] Dependencies installed
) else (
    echo [WARNING] requirements.txt not found, skipping
)
echo.

REM Step 4: Run migrations
echo ========================================
echo RUNNING MIGRATIONS
echo ========================================
echo.
echo [INFO] Creating migrations for expenses app...
python manage.py makemigrations expenses
echo.
echo [INFO] Applying all migrations...
python manage.py migrate
if %errorlevel% neq 0 (
    echo [ERROR] Migration failed!
    exit /b 1
)
echo [SUCCESS] Migrations completed
echo.

REM Step 5: Collect static files (if production flag is set)
if "%1"=="--production" (
    echo ========================================
    echo COLLECTING STATIC FILES
    echo ========================================
    echo.
    echo [INFO] Collecting static files...
    python manage.py collectstatic --noinput
    echo [SUCCESS] Static files collected
    echo.
)

REM Step 6: Create sample data
if not "%1"=="--skip-sample-data" (
    echo ========================================
    echo SAMPLE DATA
    echo ========================================
    echo.
    set /p CREATE_SAMPLE="Do you want to create sample expense data? (y/n): "
    if /i "%CREATE_SAMPLE%"=="y" (
        echo [INFO] Creating sample data...
        python create_sample_expenses.py
        echo [SUCCESS] Sample data created
    ) else (
        echo [INFO] Skipping sample data creation
    )
    echo.
)

REM Step 7: Run system check
echo ========================================
echo SYSTEM CHECK
echo ========================================
echo.
echo [INFO] Running Django system check...
python manage.py check
if %errorlevel% neq 0 (
    echo [WARNING] System check had warnings
) else (
    echo [SUCCESS] System check passed
)
echo.

REM Step 8: Verify installation
echo ========================================
echo VERIFICATION
echo ========================================
echo.
echo [INFO] Verifying expenses installation...
python verify_expenses.py
if %errorlevel% neq 0 (
    echo [WARNING] Verification had issues
) else (
    echo [SUCCESS] Verification completed
)
echo.

REM Deployment summary
echo ========================================
echo DEPLOYMENT COMPLETE
echo ========================================
echo.
echo [SUCCESS] Expenses Management System deployed successfully!
echo.
echo Next Steps:
echo 1. Start the server: python manage.py runserver
echo 2. Navigate to: http://localhost:8000/expenses/
echo 3. Login and test the features
echo.
echo Available URLs:
echo   - Expenses List:      /expenses/
echo   - Add Expense:        /expenses/add/
echo   - Pending Approvals:  /expenses/approvals/pending/
echo   - Analytics:          /expenses/analytics/
echo.
echo Documentation:
echo   - User Guide:         EXPENSES_USER_GUIDE.md
echo   - Technical Docs:     EXPENSES_FEATURE_DOCUMENTATION.md
echo   - Quick Start:        EXPENSES_README.md
echo.
echo Deployment completed at: %date% %time%
echo.
pause
