#!/bin/bash

# Switch Django backend to use SQLite instead of PostgreSQL

BACKEND_DIR="/var/www/GNX-WEB/backEnd"
BACKEND_ENV="$BACKEND_DIR/.env"

echo "Switching to SQLite database..."

if [ -f "$BACKEND_ENV" ]; then
    # Comment out or remove DATABASE_URL line
    if grep -q "^DATABASE_URL=" "$BACKEND_ENV"; then
        echo "Commenting out DATABASE_URL to use SQLite..."
        sed -i 's|^DATABASE_URL=.*|# DATABASE_URL=  # Using SQLite instead|' "$BACKEND_ENV"
        echo "✓ DATABASE_URL commented out"
    fi
    
    # Ensure db.sqlite3 path is correct (it should be in backEnd directory)
    echo ""
    echo "SQLite database will be at: $BACKEND_DIR/db.sqlite3"
    echo ""
    echo "Restarting backend to apply changes..."
    pm2 restart gnxsoft-backend
    echo "✓ Backend restarted"
    echo ""
    echo "Checking database connection..."
    cd "$BACKEND_DIR"
    source venv/bin/activate
    python manage.py check --database default
else
    echo "Error: .env file not found at $BACKEND_ENV"
    exit 1
fi

