"""
Test the Django app to see what's causing the 500 error
"""
import os
import django

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from django.contrib.auth import get_user_model
from users.models import Branch

User = get_user_model()

print("Testing database connection...")

# Check if we have any users
user_count = User.objects.count()
print(f"✓ Users in database: {user_count}")

# Check if we have any branches
branch_count = Branch.objects.count()
print(f"✓ Branches in database: {branch_count}")

# If no users, create a superuser
if user_count == 0:
    print("\n⚠️  No users found. Creating superuser...")
    print("Username: admin")
    print("Email: admin@example.com")
    print("Password: admin123")
    
    User.objects.create_superuser(
        username='admin',
        email='admin@example.com',
        password='admin123',
        first_name='Admin',
        last_name='User'
    )
    print("✓ Superuser created!")

# If no branches, create a main branch
if branch_count == 0:
    print("\n⚠️  No branches found. Creating main branch...")
    Branch.objects.create(
        name='Main Branch',
        code='MAIN',
        is_default=True
    )
    print("✓ Main branch created!")

print("\n" + "="*50)
print("✓ Database is working correctly!")
print("="*50)
print("\nYou can now access the app at: http://127.0.0.1:8000/")
print("Login with: admin / admin123")
