from django.core.management.base import BaseCommand
from users.models import RolePermission


class Command(BaseCommand):
    help = 'Set up default permissions for different user roles'

    def handle(self, *args, **options):
        # Clear existing role permissions
        RolePermission.objects.all().delete()
        
        # Define default permissions for each role
        role_permissions = {
            'admin': {
                'users': ['view', 'create', 'edit', 'delete', 'manage'],
                'loans': ['view', 'create', 'edit', 'delete', 'approve', 'reject', 'manage'],
                'reports': ['view', 'export', 'manage'],
                'documents': ['view', 'create', 'edit', 'delete', 'manage'],
                'settings': ['view', 'edit', 'manage'],
                'notifications': ['view', 'create', 'edit', 'delete', 'manage'],
                'audit': ['view', 'export', 'manage'],
                'kyc': ['view', 'create', 'edit', 'delete', 'approve', 'reject', 'manage'],
                'payments': ['view', 'create', 'edit', 'delete', 'manage'],
                'communications': ['view', 'create', 'edit', 'delete', 'manage'],
            },
            'team_leader': {
                'users': ['view', 'create', 'edit'],
                'loans': ['view', 'create', 'edit', 'approve', 'reject'],
                'reports': ['view', 'export'],
                'documents': ['view', 'create', 'edit'],
                'settings': ['view'],
                'notifications': ['view', 'create'],
                'audit': ['view'],
                'kyc': ['view', 'create', 'edit', 'approve', 'reject'],
                'payments': ['view', 'create', 'edit'],
                'communications': ['view', 'create'],
            },
            'loan_officer': {
                'users': ['view', 'create'],
                'loans': ['view', 'create', 'edit'],
                'reports': ['view'],
                'documents': ['view', 'create'],
                'settings': ['view'],
                'notifications': ['view'],
                'audit': ['view'],
                'kyc': ['view', 'create', 'edit'],
                'payments': ['view'],
                'communications': ['view'],
            },
            'secretary': {
                'users': ['view'],
                'loans': ['view'],
                'reports': ['view'],
                'documents': ['view', 'create'],
                'settings': ['view'],
                'notifications': ['view'],
                'audit': ['view'],
                'kyc': ['view'],
                'payments': ['view'],
                'communications': ['view'],
            },
            'borrower': {
                'loans': ['view'],
                'documents': ['view'],
                'notifications': ['view'],
                'payments': ['view'],
            },
        }
        
        # Create permissions
        created_count = 0
        for role, modules in role_permissions.items():
            for module, actions in modules.items():
                for action in actions:
                    RolePermission.objects.create(
                        role=role,
                        module=module,
                        action=action,
                        is_allowed=True
                    )
                    created_count += 1
        
        self.stdout.write(
            self.style.SUCCESS(
                f'Successfully created {created_count} role permissions'
            )
        )
        
        # Display summary
        for role in role_permissions.keys():
            count = RolePermission.objects.filter(role=role).count()
            self.stdout.write(f"  {role}: {count} permissions") 