#!/usr/bin/env python
"""Test if the pending clients view works correctly"""

import os
import sys
import django
from pathlib import Path

# Setup Django
sys.path.insert(0, str(Path(__file__).resolve().parent))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'branch_system.settings')
django.setup()

from users.models import CustomUser
from django.db.models import Q

def test_pending_clients_view():
    """Test the pending clients query"""
    
    print("=" * 80)
    print("TESTING PENDING CLIENTS VIEW")
    print("=" * 80)
    
    # Simulate the pending_clients view query
    pending_clients_qs = CustomUser.objects.filter(
        role='borrower', 
        status='pending_approval'
    ).select_related()
    
    total_count = pending_clients_qs.count()
    
    print(f"\n[OK] Found {total_count} pending clients in database")
    print("\nPending Clients List:")
    print("-" * 80)
    
    for client in pending_clients_qs[:10]:  # Show first 10
        print(f"\n  Name: {client.get_full_name()}")
        print(f"  ID Number: {client.id_number}")
        print(f"  Phone: {client.phone_number}")
        print(f"  Email: {client.email or 'No email'}")
        print(f"  Business: {client.business_name or 'N/A'}")
        print(f"  Branch: {client.branch.name if client.branch else 'None'}")
        print(f"  Status: {client.status}")
        print(f"  Created: {client.date_joined.strftime('%Y-%m-%d %H:%M')}")
        print("-" * 80)
    
    if total_count == 0:
        print("\n[WARNING] No pending clients found!")
        print("Create a test client using: python create_test_pending_client.py")
    else:
        print(f"\n[OK] These clients will appear in the Pending Approvals page")
        print("\nTo test in browser:")
        print("1. Start the development server: python manage.py runserver")
        print("2. Login as admin")
        print("3. Navigate to: http://localhost:8000/users/clients/")
        print("4. Click the yellow 'Pending Approvals' button")
        print("5. You should see the pending clients listed")
    
    print("\n" + "=" * 80)
    
    return total_count

if __name__ == '__main__':
    test_pending_clients_view()

