#!/bin/bash
# Script to clean up test users from Django database
# Usage: ./cleanup_test_users.sh [email]
# If no email provided, cleans up all users with email starting with 'test_'

EMAIL="$1"

cd /Users/treyt/Desktop/code/MyCrib/myCribAPI

if [ -n "$EMAIL" ]; then
    FILTER="email='$EMAIL'"
else
    FILTER="email__startswith='test_'"
fi

# Try docker exec first (if running in Docker)
if docker ps --format '{{.Names}}' | grep -q 'mycrib-web\|myCrib-web'; then
    CONTAINER_NAME=$(docker ps --format '{{.Names}}' | grep -E 'mycrib-web|myCrib-web' | head -1)
    docker exec "$CONTAINER_NAME" python manage.py shell -c "
from django.contrib.auth import get_user_model
User = get_user_model()
deleted = User.objects.filter($FILTER).delete()
print(f'Deleted: {deleted}')
" 2>/dev/null
else
    # Fallback to local Python
    export DJANGO_SETTINGS_MODULE=myCrib.settings
    python manage.py shell -c "
from django.contrib.auth import get_user_model
User = get_user_model()
deleted = User.objects.filter($FILTER).delete()
print(f'Deleted: {deleted}')
" 2>/dev/null
fi

echo "Test users cleanup complete"
