"""
Test script to verify portfolio dashboard functionality
"""
import os
import django

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

from django.test import RequestFactory
from users.portfolio_views import portfolio_dashboard
from users.models import CustomUser

def test_portfolio_dashboard():
    """Test that portfolio dashboard can be accessed"""
    factory = RequestFactory()
    
    # Try to get an admin user
    try:
        admin_user = CustomUser.objects.filter(role='admin', status='active').first()
        if not admin_user:
            print("❌ No admin user found. Creating test admin...")
            admin_user = CustomUser.objects.create_user(
                username='test_admin',
                email='test@example.com',
                password='testpass123',
                role='admin',
                status='active',
                first_name='Test',
                last_name='Admin'
            )
            print("✅ Test admin created")
        
        # Create a mock request
        request = factory.get('/portfolio/')
        request.user = admin_user
        
        # Test the view
        print("\n🔍 Testing portfolio dashboard...")
        response = portfolio_dashboard(request)
        
        if response.status_code == 200:
            print("✅ Portfolio dashboard loaded successfully!")
            print(f"   Status code: {response.status_code}")
            return True
        else:
            print(f"❌ Portfolio dashboard returned status code: {response.status_code}")
            return False
            
    except Exception as e:
        print(f"❌ Error testing portfolio dashboard: {e}")
        import traceback
        traceback.print_exc()
        return False

if __name__ == '__main__':
    print("=" * 60)
    print("Portfolio Dashboard Test")
    print("=" * 60)
    
    success = test_portfolio_dashboard()
    
    print("\n" + "=" * 60)
    if success:
        print("✅ All tests passed!")
    else:
        print("❌ Tests failed. Check the errors above.")
    print("=" * 60)
