import os
import django

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

from django.test import Client
from users.models import CustomUser

# Login as admin
client = Client()
admin = CustomUser.objects.filter(username='admin').first()

if not admin:
    print("Admin not found")
    exit(1)

# Login
response = client.post('/login/', {
    'username': 'admin',
    'password': 'admin123'
})

print(f"Login status: {response.status_code}")

# Try to access loans list
response = client.get('/loans/')
print(f"Loans list status: {response.status_code}")

# Check if there are any loan applications
from loans.models import LoanApplication, Loan
apps = LoanApplication.objects.all().count()
loans = Loan.objects.all().count()
print(f"Total applications: {apps}")
print(f"Total loans: {loans}")

# Try with generate_report
try:
    response = client.get('/loans/?generate_report=true')
    print(f"Generate report status: {response.status_code}")
    if response.status_code == 500:
        print("Error generating report")
except Exception as e:
    print(f"Exception: {e}")
    import traceback
    traceback.print_exc()
