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


class Command(BaseCommand):
    help = 'Set up page-specific permissions with granular controls for each page'

    def handle(self, *args, **options):
        # Clear existing permissions
        RolePermission.objects.all().delete()
        DefaultRolePermission.objects.all().delete()
        
        self.stdout.write('Setting up page-specific permissions...')
        
        # Define page-specific permissions for each role
        role_permissions = {
            'admin': {
                # Admin gets all permissions
                'dashboard': ['access', 'view', 'manage', 'configure'],
                'dashboard_overview': ['access', 'view', 'manage'],
                'dashboard_metrics': ['access', 'view', 'manage', 'configure'],
                'dashboard_analytics': ['access', 'view', 'manage', 'configure'],
                'dashboard_widgets': ['access', 'view', 'manage', 'configure'],
                
                'clients': ['access', 'view', 'create', 'edit', 'delete', 'manage', 'assign', 'reassign'],
                'clients_list': ['access', 'view', 'create', 'edit', 'delete', 'export', 'import'],
                'clients_create': ['access', 'create', 'manage'],
                'clients_edit': ['access', 'edit', 'manage'],
                'clients_delete': ['access', 'delete', 'permanently_delete'],
                'clients_view_details': ['access', 'view', 'manage'],
                'clients_loan_history': ['access', 'view', 'export', 'print'],
                'clients_documents': ['access', 'view', 'upload', 'download', 'delete', 'manage'],
                'clients_kyc': ['access', 'view', 'verify', 'approve', 'reject', 'manage'],
                'clients_assign': ['access', 'assign', 'reassign', 'manage'],
                
                'loans': ['access', 'view', 'create', 'edit', 'delete', 'approve', 'reject', 'manage'],
                'loans_list': ['access', 'view', 'create', 'edit', 'delete', 'export', 'import'],
                'loans_create': ['access', 'create', 'manage'],
                'loans_edit': ['access', 'edit', 'manage'],
                'loans_delete': ['access', 'delete', 'permanently_delete'],
                'loans_approve': ['access', 'approve', 'manage'],
                'loans_reject': ['access', 'reject', 'manage'],
                'loans_rollover': ['access', 'create', 'approve', 'reject', 'manage'],
                'loans_view_details': ['access', 'view', 'manage'],
                'loans_amortization': ['access', 'view', 'generate', 'print'],
                'loans_receipt': ['access', 'view', 'generate', 'print', 'download'],
                
                'repayments': ['access', 'view', 'create', 'edit', 'delete', 'manage', 'verify', 'reconcile'],
                'repayments_list': ['access', 'view', 'create', 'edit', 'delete', 'export'],
                'repayments_record': ['access', 'create', 'manage'],
                'repayments_edit': ['access', 'edit', 'manage'],
                'repayments_delete': ['access', 'delete', 'permanently_delete'],
                'repayments_export': ['access', 'export', 'download'],
                'repayments_verify': ['access', 'verify', 'manage'],
                'repayments_reconcile': ['access', 'reconcile', 'manage'],
                
                'portfolio': ['access', 'view', 'manage', 'assign', 'reassign', 'configure'],
                'portfolio_overview': ['access', 'view', 'manage'],
                'portfolio_managers': ['access', 'view', 'create', 'edit', 'delete', 'manage'],
                'portfolio_performance': ['access', 'view', 'export', 'generate'],
                'portfolio_assignments': ['access', 'view', 'assign', 'reassign', 'manage'],
                'portfolio_analytics': ['access', 'view', 'export', 'generate'],
                'portfolio_reassign': ['access', 'reassign', 'manage'],
                
                'reports_statements': ['access', 'view', 'generate', 'export', 'manage', 'configure'],
                'reports_dashboard': ['access', 'view', 'manage', 'configure'],
                'reports_loans_due': ['access', 'view', 'export', 'generate', 'print'],
                'reports_delinquent': ['access', 'view', 'export', 'generate', 'print'],
                'reports_arrears': ['access', 'view', 'export', 'generate', 'print'],
                'reports_processing_fees': ['access', 'view', 'export', 'generate', 'print'],
                'reports_interest_income': ['access', 'view', 'export', 'generate', 'print'],
                'reports_registration_fees': ['access', 'view', 'export', 'generate', 'print'],
                'reports_customer_requests': ['access', 'view', 'export', 'generate', 'print'],
                'reports_export': ['access', 'export', 'download'],
                'reports_generate': ['access', 'generate', 'manage'],
                'statements_generate': ['access', 'generate', 'manage'],
                'statements_download': ['access', 'download', 'print'],
                
                'documents': ['access', 'view', 'upload', 'download', 'delete', 'share', 'manage'],
                'documents_upload': ['access', 'upload', 'manage'],
                'documents_download': ['access', 'download', 'print'],
                'documents_delete': ['access', 'delete', 'permanently_delete'],
                'documents_share': ['access', 'share', 'manage'],
                'documents_organize': ['access', 'edit', 'manage'],
                'documents_search': ['access', 'view', 'manage'],
                
                'customer_documents': ['access', 'view', 'upload', 'verify', 'approve', 'manage'],
                'customer_docs_view': ['access', 'view', 'download'],
                'customer_docs_upload': ['access', 'upload', 'manage'],
                'customer_docs_verify': ['access', 'verify', 'manage'],
                'customer_docs_approve': ['access', 'approve', 'reject', 'manage'],
                
                'payment_receipts': ['access', 'view', 'generate', 'print', 'download', 'edit', 'delete', 'manage'],
                'receipts_view': ['access', 'view', 'download'],
                'receipts_generate': ['access', 'generate', 'manage'],
                'receipts_print': ['access', 'print', 'download'],
                'receipts_download': ['access', 'download', 'print'],
                'receipts_edit': ['access', 'edit', 'manage'],
                'receipts_delete': ['access', 'delete', 'permanently_delete'],
                
                'notifications': ['access', 'view', 'send', 'manage', 'configure'],
                'notifications_view': ['access', 'view', 'manage'],
                'notifications_send': ['access', 'send', 'notify', 'manage'],
                'notifications_manage': ['access', 'manage', 'configure'],
                'notifications_settings': ['access', 'configure', 'manage'],
                
                'settings': ['access', 'view', 'configure', 'manage'],
                'settings_system': ['access', 'configure', 'manage'],
                'settings_branch': ['access', 'configure', 'manage'],
                'settings_staff': ['access', 'view', 'create', 'edit', 'delete', 'manage'],
                'settings_permissions': ['access', 'view', 'edit', 'manage'],
                'settings_backup': ['access', 'configure', 'backup', 'restore', 'manage'],
                'settings_integrations': ['access', 'configure', 'manage'],
                
                # System Administration
                'users': ['access', 'view', 'create', 'edit', 'delete', 'suspend', 'activate', 'manage'],
                'audit': ['access', 'view', 'export', 'monitor', 'manage'],
                'kyc': ['access', 'view', 'create', 'edit', 'delete', 'verify', 'approve', 'reject', 'manage'],
                'communications': ['access', 'view', 'send', 'manage'],
                'backup': ['access', 'backup', 'restore', 'manage'],
                'system': ['access', 'view', 'configure', 'monitor', 'manage'],
                'maintenance': ['access', 'view', 'maintain', 'configure', 'manage'],
                'api': ['access', 'view', 'configure', 'manage'],
                'integrations': ['access', 'view', 'configure', 'manage'],
            },
            
            'team_leader': {
                # Team Leader gets management permissions but not system admin
                'dashboard': ['access', 'view', 'manage'],
                'dashboard_overview': ['access', 'view', 'manage'],
                'dashboard_metrics': ['access', 'view', 'manage'],
                'dashboard_analytics': ['access', 'view', 'manage'],
                'dashboard_widgets': ['access', 'view'],
                
                'clients': ['access', 'view', 'create', 'edit', 'manage', 'assign', 'reassign'],
                'clients_list': ['access', 'view', 'create', 'edit', 'export'],
                'clients_create': ['access', 'create', 'manage'],
                'clients_edit': ['access', 'edit', 'manage'],
                'clients_delete': ['access', 'delete'],
                'clients_view_details': ['access', 'view', 'manage'],
                'clients_loan_history': ['access', 'view', 'export', 'print'],
                'clients_documents': ['access', 'view', 'upload', 'download', 'manage'],
                'clients_kyc': ['access', 'view', 'verify', 'approve', 'reject', 'manage'],
                'clients_assign': ['access', 'assign', 'reassign', 'manage'],
                
                'loans': ['access', 'view', 'create', 'edit', 'approve', 'reject', 'manage'],
                'loans_list': ['access', 'view', 'create', 'edit', 'export'],
                'loans_create': ['access', 'create', 'manage'],
                'loans_edit': ['access', 'edit', 'manage'],
                'loans_delete': ['access', 'delete'],
                'loans_approve': ['access', 'approve', 'manage'],
                'loans_reject': ['access', 'reject', 'manage'],
                'loans_rollover': ['access', 'create', 'approve', 'reject', 'manage'],
                'loans_view_details': ['access', 'view', 'manage'],
                'loans_amortization': ['access', 'view', 'generate', 'print'],
                'loans_receipt': ['access', 'view', 'generate', 'print', 'download'],
                
                'repayments': ['access', 'view', 'create', 'edit', 'manage', 'verify', 'reconcile'],
                'repayments_list': ['access', 'view', 'create', 'edit', 'export'],
                'repayments_record': ['access', 'create', 'manage'],
                'repayments_edit': ['access', 'edit', 'manage'],
                'repayments_delete': ['access', 'delete'],
                'repayments_export': ['access', 'export', 'download'],
                'repayments_verify': ['access', 'verify', 'manage'],
                'repayments_reconcile': ['access', 'reconcile', 'manage'],
                
                'portfolio': ['access', 'view', 'manage', 'assign', 'reassign'],
                'portfolio_overview': ['access', 'view', 'manage'],
                'portfolio_managers': ['access', 'view', 'create', 'edit', 'manage'],
                'portfolio_performance': ['access', 'view', 'export', 'generate'],
                'portfolio_assignments': ['access', 'view', 'assign', 'reassign', 'manage'],
                'portfolio_analytics': ['access', 'view', 'export', 'generate'],
                'portfolio_reassign': ['access', 'reassign', 'manage'],
                
                'reports_statements': ['access', 'view', 'generate', 'export', 'manage'],
                'reports_dashboard': ['access', 'view', 'manage'],
                'reports_loans_due': ['access', 'view', 'export', 'generate', 'print'],
                'reports_delinquent': ['access', 'view', 'export', 'generate', 'print'],
                'reports_arrears': ['access', 'view', 'export', 'generate', 'print'],
                'reports_processing_fees': ['access', 'view', 'export', 'generate', 'print'],
                'reports_interest_income': ['access', 'view', 'export', 'generate', 'print'],
                'reports_registration_fees': ['access', 'view', 'export', 'generate', 'print'],
                'reports_customer_requests': ['access', 'view', 'export', 'generate', 'print'],
                'reports_export': ['access', 'export', 'download'],
                'reports_generate': ['access', 'generate', 'manage'],
                'statements_generate': ['access', 'generate', 'manage'],
                'statements_download': ['access', 'download', 'print'],
                
                'documents': ['access', 'view', 'upload', 'download', 'share', 'manage'],
                'documents_upload': ['access', 'upload', 'manage'],
                'documents_download': ['access', 'download', 'print'],
                'documents_delete': ['access', 'delete'],
                'documents_share': ['access', 'share', 'manage'],
                'documents_organize': ['access', 'edit', 'manage'],
                'documents_search': ['access', 'view', 'manage'],
                
                'customer_documents': ['access', 'view', 'upload', 'verify', 'approve', 'manage'],
                'customer_docs_view': ['access', 'view', 'download'],
                'customer_docs_upload': ['access', 'upload', 'manage'],
                'customer_docs_verify': ['access', 'verify', 'manage'],
                'customer_docs_approve': ['access', 'approve', 'reject', 'manage'],
                
                'payment_receipts': ['access', 'view', 'generate', 'print', 'download', 'edit', 'manage'],
                'receipts_view': ['access', 'view', 'download'],
                'receipts_generate': ['access', 'generate', 'manage'],
                'receipts_print': ['access', 'print', 'download'],
                'receipts_download': ['access', 'download', 'print'],
                'receipts_edit': ['access', 'edit', 'manage'],
                'receipts_delete': ['access', 'delete'],
                
                'notifications': ['access', 'view', 'send', 'manage'],
                'notifications_view': ['access', 'view', 'manage'],
                'notifications_send': ['access', 'send', 'notify', 'manage'],
                'notifications_manage': ['access', 'manage'],
                'notifications_settings': ['access', 'view'],
                
                'settings': ['access', 'view'],
                'settings_system': [],
                'settings_branch': ['access', 'view'],
                'settings_staff': ['access', 'view', 'create', 'edit', 'manage'],
                'settings_permissions': ['access', 'view', 'edit', 'manage'],
                'settings_backup': [],
                'settings_integrations': [],
                
                # System Administration - Limited access
                'users': ['access', 'view', 'create', 'edit', 'manage'],
                'audit': ['access', 'view', 'export'],
                'kyc': ['access', 'view', 'create', 'edit', 'verify', 'approve', 'reject', 'manage'],
                'communications': ['access', 'view', 'send', 'manage'],
                'backup': [],
                'system': [],
                'maintenance': [],
                'api': [],
                'integrations': [],
            },
            
            'loan_officer': {
                # Loan Officer gets operational permissions
                'dashboard': ['access', 'view'],
                'dashboard_overview': ['access', 'view'],
                'dashboard_metrics': ['access', 'view'],
                'dashboard_analytics': ['access', 'view'],
                'dashboard_widgets': ['access', 'view'],
                
                'clients': ['access', 'view', 'create', 'edit'],
                'clients_list': ['access', 'view', 'create', 'edit', 'export'],
                'clients_create': ['access', 'create'],
                'clients_edit': ['access', 'edit'],
                'clients_delete': [],
                'clients_view_details': ['access', 'view'],
                'clients_loan_history': ['access', 'view', 'export', 'print'],
                'clients_documents': ['access', 'view', 'upload', 'download'],
                'clients_kyc': ['access', 'view', 'create', 'edit', 'verify'],
                'clients_assign': [],
                
                'loans': ['access', 'view', 'create', 'edit'],
                'loans_list': ['access', 'view', 'create', 'edit', 'export'],
                'loans_create': ['access', 'create'],
                'loans_edit': ['access', 'edit'],
                'loans_delete': [],
                'loans_approve': [],
                'loans_reject': [],
                'loans_rollover': ['access', 'create'],
                'loans_view_details': ['access', 'view'],
                'loans_amortization': ['access', 'view', 'print'],
                'loans_receipt': ['access', 'view', 'print', 'download'],
                
                'repayments': ['access', 'view', 'create', 'edit'],
                'repayments_list': ['access', 'view', 'create', 'edit', 'export'],
                'repayments_record': ['access', 'create'],
                'repayments_edit': ['access', 'edit'],
                'repayments_delete': [],
                'repayments_export': ['access', 'export', 'download'],
                'repayments_verify': ['access', 'verify'],
                'repayments_reconcile': [],
                
                'portfolio': ['access', 'view'],
                'portfolio_overview': ['access', 'view'],
                'portfolio_managers': ['access', 'view'],
                'portfolio_performance': ['access', 'view'],
                'portfolio_assignments': ['access', 'view'],
                'portfolio_analytics': ['access', 'view'],
                'portfolio_reassign': [],
                
                'reports_statements': ['access', 'view', 'export'],
                'reports_dashboard': ['access', 'view'],
                'reports_loans_due': ['access', 'view', 'export', 'print'],
                'reports_delinquent': ['access', 'view', 'export', 'print'],
                'reports_arrears': ['access', 'view', 'export', 'print'],
                'reports_processing_fees': ['access', 'view', 'export', 'print'],
                'reports_interest_income': ['access', 'view', 'export', 'print'],
                'reports_registration_fees': ['access', 'view', 'export', 'print'],
                'reports_customer_requests': ['access', 'view', 'export', 'print'],
                'reports_export': ['access', 'export', 'download'],
                'reports_generate': [],
                'statements_generate': [],
                'statements_download': ['access', 'download', 'print'],
                
                'documents': ['access', 'view', 'upload', 'download'],
                'documents_upload': ['access', 'upload'],
                'documents_download': ['access', 'download', 'print'],
                'documents_delete': [],
                'documents_share': [],
                'documents_organize': [],
                'documents_search': ['access', 'view'],
                
                'customer_documents': ['access', 'view', 'upload'],
                'customer_docs_view': ['access', 'view', 'download'],
                'customer_docs_upload': ['access', 'upload'],
                'customer_docs_verify': ['access', 'verify'],
                'customer_docs_approve': [],
                
                'payment_receipts': ['access', 'view', 'generate', 'print', 'download'],
                'receipts_view': ['access', 'view', 'download'],
                'receipts_generate': ['access', 'generate'],
                'receipts_print': ['access', 'print', 'download'],
                'receipts_download': ['access', 'download', 'print'],
                'receipts_edit': [],
                'receipts_delete': [],
                
                'notifications': ['access', 'view', 'send'],
                'notifications_view': ['access', 'view'],
                'notifications_send': ['access', 'send', 'notify'],
                'notifications_manage': [],
                'notifications_settings': [],
                
                'settings': [],
                'settings_system': [],
                'settings_branch': [],
                'settings_staff': [],
                'settings_permissions': [],
                'settings_backup': [],
                'settings_integrations': [],
                
                # System Administration - No access
                'users': [],
                'audit': [],
                'kyc': ['access', 'view', 'create', 'edit', 'verify'],
                'communications': ['access', 'view', 'send'],
                'backup': [],
                'system': [],
                'maintenance': [],
                'api': [],
                'integrations': [],
            },
            
            'secretary': {
                # Secretary gets administrative and documentation permissions
                'dashboard': ['access', 'view'],
                'dashboard_overview': ['access', 'view'],
                'dashboard_metrics': ['access', 'view'],
                'dashboard_analytics': ['access', 'view'],
                'dashboard_widgets': ['access', 'view'],
                
                'clients': ['access', 'view', 'create', 'edit'],
                'clients_list': ['access', 'view', 'create', 'edit', 'export'],
                'clients_create': ['access', 'create'],
                'clients_edit': ['access', 'edit'],
                'clients_delete': [],
                'clients_view_details': ['access', 'view'],
                'clients_loan_history': ['access', 'view', 'export', 'print'],
                'clients_documents': ['access', 'view', 'upload', 'download', 'delete', 'manage'],
                'clients_kyc': ['access', 'view', 'create', 'edit'],
                'clients_assign': [],
                
                'loans': ['access', 'view'],
                'loans_list': ['access', 'view', 'export'],
                'loans_create': [],
                'loans_edit': [],
                'loans_delete': [],
                'loans_approve': [],
                'loans_reject': [],
                'loans_rollover': [],
                'loans_view_details': ['access', 'view'],
                'loans_amortization': ['access', 'view', 'print'],
                'loans_receipt': ['access', 'view', 'print', 'download'],
                
                'repayments': ['access', 'view', 'create', 'edit'],
                'repayments_list': ['access', 'view', 'create', 'edit', 'export'],
                'repayments_record': ['access', 'create'],
                'repayments_edit': ['access', 'edit'],
                'repayments_delete': [],
                'repayments_export': ['access', 'export', 'download'],
                'repayments_verify': [],
                'repayments_reconcile': [],
                
                'portfolio': ['access', 'view'],
                'portfolio_overview': ['access', 'view'],
                'portfolio_managers': ['access', 'view'],
                'portfolio_performance': ['access', 'view'],
                'portfolio_assignments': ['access', 'view'],
                'portfolio_analytics': ['access', 'view'],
                'portfolio_reassign': [],
                
                'reports_statements': ['access', 'view', 'export'],
                'reports_dashboard': ['access', 'view'],
                'reports_loans_due': ['access', 'view', 'export', 'print'],
                'reports_delinquent': ['access', 'view', 'export', 'print'],
                'reports_arrears': ['access', 'view', 'export', 'print'],
                'reports_processing_fees': ['access', 'view', 'export', 'print'],
                'reports_interest_income': ['access', 'view', 'export', 'print'],
                'reports_registration_fees': ['access', 'view', 'export', 'print'],
                'reports_customer_requests': ['access', 'view', 'export', 'print'],
                'reports_export': ['access', 'export', 'download'],
                'reports_generate': [],
                'statements_generate': ['access', 'generate'],
                'statements_download': ['access', 'download', 'print'],
                
                'documents': ['access', 'view', 'upload', 'download', 'delete', 'share', 'manage'],
                'documents_upload': ['access', 'upload', 'manage'],
                'documents_download': ['access', 'download', 'print'],
                'documents_delete': ['access', 'delete'],
                'documents_share': ['access', 'share', 'manage'],
                'documents_organize': ['access', 'edit', 'manage'],
                'documents_search': ['access', 'view', 'manage'],
                
                'customer_documents': ['access', 'view', 'upload', 'manage'],
                'customer_docs_view': ['access', 'view', 'download'],
                'customer_docs_upload': ['access', 'upload', 'manage'],
                'customer_docs_verify': [],
                'customer_docs_approve': [],
                
                'payment_receipts': ['access', 'view', 'generate', 'print', 'download', 'edit', 'delete', 'manage'],
                'receipts_view': ['access', 'view', 'download'],
                'receipts_generate': ['access', 'generate', 'manage'],
                'receipts_print': ['access', 'print', 'download'],
                'receipts_download': ['access', 'download', 'print'],
                'receipts_edit': ['access', 'edit', 'manage'],
                'receipts_delete': ['access', 'delete'],
                
                'notifications': ['access', 'view', 'send'],
                'notifications_view': ['access', 'view'],
                'notifications_send': ['access', 'send', 'notify'],
                'notifications_manage': [],
                'notifications_settings': [],
                
                'settings': [],
                'settings_system': [],
                'settings_branch': [],
                'settings_staff': [],
                'settings_permissions': [],
                'settings_backup': [],
                'settings_integrations': [],
                
                # System Administration - No access
                'users': [],
                'audit': [],
                'kyc': ['access', 'view', 'create', 'edit'],
                'communications': ['access', 'view', 'send'],
                'backup': [],
                'system': [],
                'maintenance': [],
                'api': [],
                'integrations': [],
            },
            
            'borrower': {
                # Borrowers have very limited access
                'dashboard': ['access', 'view'],
                'dashboard_overview': ['access', 'view'],
                'dashboard_metrics': [],
                'dashboard_analytics': [],
                'dashboard_widgets': [],
                
                'clients': ['access', 'view'],
                'clients_list': [],
                'clients_create': [],
                'clients_edit': [],
                'clients_delete': [],
                'clients_view_details': ['access', 'view'],
                'clients_loan_history': [],
                'clients_documents': [],
                'clients_kyc': [],
                'clients_assign': [],
                
                'loans': ['access', 'view'],
                'loans_list': ['access', 'view'],
                'loans_create': [],
                'loans_edit': [],
                'loans_delete': [],
                'loans_approve': [],
                'loans_reject': [],
                'loans_rollover': ['access', 'create'],
                'loans_view_details': ['access', 'view'],
                'loans_amortization': ['access', 'view'],
                'loans_receipt': ['access', 'view', 'download'],
                
                'repayments': ['access', 'view'],
                'repayments_list': ['access', 'view'],
                'repayments_record': [],
                'repayments_edit': [],
                'repayments_delete': [],
                'repayments_export': [],
                'repayments_verify': [],
                'repayments_reconcile': [],
                
                'portfolio': [],
                'portfolio_overview': [],
                'portfolio_managers': [],
                'portfolio_performance': [],
                'portfolio_assignments': [],
                'portfolio_analytics': [],
                'portfolio_reassign': [],
                
                'reports_statements': [],
                'reports_dashboard': [],
                'reports_loans_due': [],
                'reports_delinquent': [],
                'reports_arrears': [],
                'reports_processing_fees': [],
                'reports_interest_income': [],
                'reports_registration_fees': [],
                'reports_customer_requests': [],
                'reports_export': [],
                'reports_generate': [],
                'statements_generate': [],
                'statements_download': ['access', 'download'],
                
                'documents': ['access', 'view', 'download'],
                'documents_upload': [],
                'documents_download': ['access', 'download'],
                'documents_delete': [],
                'documents_share': [],
                'documents_organize': [],
                'documents_search': [],
                
                'customer_documents': ['access', 'view', 'upload'],
                'customer_docs_view': ['access', 'view', 'download'],
                'customer_docs_upload': ['access', 'upload'],
                'customer_docs_verify': [],
                'customer_docs_approve': [],
                
                'payment_receipts': ['access', 'view', 'download'],
                'receipts_view': ['access', 'view', 'download'],
                'receipts_generate': [],
                'receipts_print': [],
                'receipts_download': ['access', 'download'],
                'receipts_edit': [],
                'receipts_delete': [],
                
                'notifications': ['access', 'view'],
                'notifications_view': ['access', 'view'],
                'notifications_send': [],
                'notifications_manage': [],
                'notifications_settings': [],
                
                'settings': [],
                'settings_system': [],
                'settings_branch': [],
                'settings_staff': [],
                'settings_permissions': [],
                'settings_backup': [],
                'settings_integrations': [],
                
                # System Administration - No access
                'users': [],
                'audit': [],
                'kyc': ['access', 'view'],
                'communications': ['access', 'view'],
                'backup': [],
                'system': [],
                'maintenance': [],
                'api': [],
                'integrations': [],
            }
        }
        
        # Create permissions for each role
        for role, modules in role_permissions.items():
            for module, actions in modules.items():
                for action in actions:
                    # Create RolePermission
                    RolePermission.objects.get_or_create(
                        role=role,
                        module=module,
                        action=action,
                        defaults={'is_allowed': True}
                    )
                    
                    # Create DefaultRolePermission
                    DefaultRolePermission.objects.get_or_create(
                        role=role,
                        module=module,
                        action=action,
                        defaults={
                            'is_allowed': True,
                            'description': f'Default permission for {role} to {action} {module}'
                        }
                    )
        
        # Create denied permissions for actions not explicitly allowed
        all_modules = [choice[0] for choice in RolePermission.MODULE_CHOICES]
        all_actions = [choice[0] for choice in RolePermission.ACTION_CHOICES]
        
        for role in ['admin', 'team_leader', 'loan_officer', 'secretary', 'borrower']:
            for module in all_modules:
                for action in all_actions:
                    # Only create if it doesn't exist
                    if not RolePermission.objects.filter(role=role, module=module, action=action).exists():
                        RolePermission.objects.create(
                            role=role,
                            module=module,
                            action=action,
                            is_allowed=False
                        )
                    
                    if not DefaultRolePermission.objects.filter(role=role, module=module, action=action).exists():
                        DefaultRolePermission.objects.create(
                            role=role,
                            module=module,
                            action=action,
                            is_allowed=False,
                            description=f'Default permission for {role} to {action} {module}'
                        )
        
        self.stdout.write(
            self.style.SUCCESS(
                f'Successfully created page-specific permissions for {len(role_permissions)} roles '
                f'covering {len(all_modules)} modules and {len(all_actions)} actions'
            )
        )
