#!/usr/bin/env python3
"""
Direct test of the simplified permissions view
"""

import os
import sys
import django

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

from django.test import Client
from django.contrib.auth import get_user_model
from users.models import CustomUser
from users.simplified_permissions_views import simplified_user_permissions

def test_view_direct():
    print("🧪 Testing simplified permissions view directly...")
    
    # Get a test user
    users = CustomUser.objects.filter(role__in=['admin', 'team_leader', 'loan_officer']).exclude(role='borrower')
    if not users.exists():
        print("❌ No staff users found for testing")
        return False
    
    test_user = users.first()
    admin_user = CustomUser.objects.filter(role='admin').first()
    
    if not admin_user:
        print("❌ No admin user found for testing")
        return False
    
    print(f"✅ Test user: {test_user.get_full_name()} ({test_user.role})")
    print(f"✅ Admin user: {admin_user.get_full_name()} ({admin_user.role})")
    
    # Create a test client
    client = Client()
    
    # Login as admin
    client.force_login(admin_user)
    
    # Test GET request
    try:
        url = f'/simplified-permissions/{test_user.id}/'
        response = client.get(url)
        print(f"✅ GET request to {url}: Status {response.status_code}")
        
        if response.status_code == 200:
            print("✅ View rendered successfully")
            return True
        else:
            print(f"❌ View returned status {response.status_code}")
            if hasattr(response, 'content'):
                print(f"Response content: {response.content[:500]}")
            return False
            
    except Exception as e:
        print(f"❌ View test failed: {e}")
        import traceback
        traceback.print_exc()
        return False

if __name__ == '__main__':
    success = test_view_direct()
    if success:
        print("\n✅ View test passed! The simplified permissions view is working.")
    else:
        print("\n❌ View test failed.")
        sys.exit(1)