#!/bin/bash

# Script to run Django server with venv activated

cd "$(dirname "$0")"

# Activate virtual environment
if [ -d "venv" ]; then
    source venv/bin/activate
    echo "✅ Virtual environment activated"
else
    echo "⚠️  Warning: venv not found. Using system Python."
fi

# Check if all required packages are installed
echo "🔍 Checking dependencies..."
python -c "import django; import ratelimit; import firebase_admin; import google.auth" 2>&1
if [ $? -eq 0 ]; then
    echo "✅ All dependencies are installed"
else
    echo "❌ Some dependencies are missing. Installing..."
    pip install -r requirements.txt
fi

# Run migrations if needed
echo "📦 Checking migrations..."
python manage.py makemigrations --check --dry-run 2>&1 | grep -q "No changes"
if [ $? -ne 0 ]; then
    echo "⚠️  Pending migrations found. Run: python manage.py makemigrations && python manage.py migrate"
fi

# Run server
echo "🚀 Starting Django server..."
python manage.py runserver

