from django.urls import path, reverse_lazy
from django.contrib.auth import views as auth_views
from django.conf import settings
from . import views, staff_views, portfolio_views, enhanced_permissions_views, simplified_permissions_views, role_template_views, portfolio_snapshot_views, client_analytics_views, growth_forecasting_views, audit_views, client_portfolio_views

app_name = 'users'

urlpatterns = [
    # Authentication
    path('login/', views.custom_login, name='login'),
    path('logout/', views.custom_logout, name='logout'),
    
    # Filtered views
    path('clients/filtered/', views.filtered_clients, name='filtered_clients'),
    path('password_change/', auth_views.PasswordChangeView.as_view(template_name='users/password_change.html'), name='password_change'),
    path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(template_name='users/password_change_done.html'), name='password_change_done'),
    
    # Password Reset URLs
    path('password_reset/', 
         auth_views.PasswordResetView.as_view(template_name='users/password_reset.html',
                                             email_template_name='users/password_reset_email.html',
                                             subject_template_name='users/password_reset_subject.txt',
                                             success_url=reverse_lazy('users:password_reset_done'),
                                             extra_email_context={'domain': settings.PASSWORD_RESET_DOMAIN}),
         name='password_reset'),
    path('password_reset/done/', 
         auth_views.PasswordResetDoneView.as_view(template_name='users/password_reset_done.html'),
         name='password_reset_done'),
    path('reset/<uidb64>/<token>/', 
         auth_views.PasswordResetConfirmView.as_view(template_name='users/password_reset_confirm.html',
                                                   success_url=reverse_lazy('users:password_reset_complete')),
         name='password_reset_confirm'),
    path('reset/done/', 
         auth_views.PasswordResetCompleteView.as_view(template_name='users/password_reset_complete.html'),
         name='password_reset_complete'),
    
    # User profile
    path('profile/', views.profile, name='profile'),
    path('profile/edit/', views.edit_profile, name='edit_profile'),
    
    # Admin management
    path('admins/', views.admin_list, name='admin_list'),
    path('admins/create/', views.admin_create, name='admin_create'),
    path('admins/<uuid:admin_id>/', views.admin_detail, name='admin_detail'),
    path('admins/<uuid:admin_id>/update/', views.admin_update, name='admin_update'),
    path('admins/<uuid:admin_id>/delete/', views.admin_confirm_delete, name='admin_confirm_delete'),
    path('admins/<uuid:admin_id>/reactivate/', views.admin_confirm_reactivate, name='admin_confirm_reactivate'),
    path('access-logs/', views.user_access_logs, name='access_logs'),
    path('permissions/<uuid:user_id>/', views.user_permissions, name='user_permissions'),
    # Simplified permissions - must be before any catch-all patterns
    path('simplified-permissions/<uuid:user_id>/', simplified_permissions_views.simplified_user_permissions, name='simplified_user_permissions'),
    
    # Portfolio Snapshot Management
    path('portfolio/snapshots/', portfolio_snapshot_views.portfolio_snapshot_dashboard, name='portfolio_snapshot_dashboard'),
    path('portfolio/snapshots/manager/<uuid:manager_id>/', portfolio_snapshot_views.manager_portfolio_detail, name='manager_portfolio_snapshot'),
    path('portfolio/trends/<uuid:manager_id>/', portfolio_snapshot_views.portfolio_trends_api, name='portfolio_trends_api'),
    path('portfolio/generate/', portfolio_snapshot_views.generate_snapshot_manual, name='generate_snapshot_manual'),
    path('portfolio/alerts/', portfolio_snapshot_views.portfolio_health_alerts, name='portfolio_health_alerts'),
    path('portfolio/alerts/<uuid:alert_id>/read/', portfolio_snapshot_views.mark_alert_read, name='mark_alert_read'),
    path('portfolio/comparison/', portfolio_snapshot_views.portfolio_comparison, name='portfolio_comparison'),
    path('portfolio/export/<uuid:manager_id>/', portfolio_snapshot_views.export_portfolio_data, name='export_portfolio_data'),
    
    # Enhanced Staff Management
    path('staff/dashboard/', staff_views.staff_management_dashboard, name='staff_dashboard'),
    path('staff/default-permissions/', staff_views.default_permissions_management, name='default_permissions'),
    path('staff/permissions/<uuid:user_id>/', staff_views.staff_permissions_detail, name='staff_permissions_detail'),
    path('staff/apply-defaults/<uuid:user_id>/', staff_views.apply_default_permissions, name='apply_default_permissions'),
    path('staff/bulk-update/', staff_views.bulk_permission_update, name='bulk_permission_update'),
    path('staff/templates/', staff_views.permission_templates, name='permission_templates'),
    path('staff/audit-log/', staff_views.permission_audit_log, name='permission_audit_log'),
    path('staff/copy-permissions/<uuid:from_user_id>/<uuid:to_user_id>/', staff_views.copy_permissions, name='copy_permissions'),
    
    # Enhanced Permissions System
    path('staff/enhanced-permissions/<uuid:user_id>/', enhanced_permissions_views.enhanced_staff_permissions, name='enhanced_staff_permissions'),
    path('staff/enhanced-permissions/<uuid:user_id>/summary/', enhanced_permissions_views.get_permission_summary, name='get_permission_summary'),
    path('staff/enhanced-permissions/copy/<uuid:from_user_id>/<uuid:to_user_id>/', enhanced_permissions_views.copy_permissions_enhanced, name='copy_permissions_enhanced'),
    path('staff/enhanced-permissions/bulk-update/', enhanced_permissions_views.bulk_permission_update_enhanced, name='bulk_permission_update_enhanced'),
    path('staff/enhanced-permissions/templates/', enhanced_permissions_views.permission_templates_enhanced, name='permission_templates_enhanced'),
    
    # Role Template Management System
    path('role-templates/', role_template_views.role_template_configuration, name='role_template_configuration'),
    path('role-templates/save/', role_template_views.save_role_template, name='save_role_template'),
    path('role-templates/bulk-assign/', role_template_views.bulk_permission_assignment, name='bulk_permission_assignment'),
    path('role-templates/comparison/', role_template_views.role_comparison, name='role_comparison'),
    path('role-templates/clone/', role_template_views.clone_role_template, name='clone_role_template'),
    path('role-templates/matrix/', role_template_views.permission_matrix_view, name='permission_matrix_view'),
    path('api/role-template/<str:role>/', role_template_views.get_role_template_data, name='get_role_template_data'),
    
    # Role Template Application
    path('role-templates/apply/<uuid:user_id>/', role_template_views.apply_role_template_to_user, name='apply_role_template_to_user'),
    path('role-templates/bulk-apply/', role_template_views.bulk_apply_role_templates, name='bulk_apply_role_templates'),
    path('role-templates/resolve-conflicts/<uuid:user_id>/', role_template_views.resolve_permission_conflicts, name='resolve_permission_conflicts'),
    path('role-templates/rollback/create/', role_template_views.create_rollback_point, name='create_rollback_point'),
    path('role-templates/rollback/execute/', role_template_views.rollback_template_changes, name='rollback_template_changes'),
    
    # AJAX endpoints
    path('api/role-permissions/<str:role>/', staff_views.get_role_permissions, name='get_role_permissions'),
    path('api/simplified-role-permissions/<str:role>/', simplified_permissions_views.get_simplified_role_permissions, name='get_simplified_role_permissions'),
    path('api/bulk-update-simplified-permissions/', simplified_permissions_views.bulk_update_simplified_permissions, name='bulk_update_simplified_permissions'),
    
    # Client management
    path('clients/', views.client_list, name='client_list'),
    path('clients/create/', views.client_create, name='client_create'),
    path('clients/pending/', views.pending_clients, name='pending_clients'),
    path('clients/rejected/', views.rejected_clients, name='rejected_clients'),
    path('clients/<uuid:client_id>/', views.client_detail, name='client_detail'),
    path('clients/<uuid:client_id>/history/', views.client_complete_history, name='client_complete_history'),
    path('clients/<uuid:client_id>/update/', views.client_update, name='client_update'),
    path('clients/<uuid:client_id>/delete/', views.client_delete, name='client_delete'),
    path('clients/<uuid:client_id>/approve/', views.approve_client, name='approve_client'),
    path('clients/<uuid:client_id>/reject/', views.reject_client, name='reject_client'),
    path('clients/bulk-delete/', views.client_bulk_delete, name='client_bulk_delete'),
    path('clients/bulk-approve/', views.bulk_approve_clients, name='bulk_approve_clients'),
    path('clients/<uuid:client_id>/delete/api/', views.client_delete_api, name='client_delete_api'),
    path('clients/<uuid:client_id>/documents/', views.client_documents, name='client_documents'),
    path('clients/<uuid:client_id>/documents/upload/', views.upload_client_document, name='upload_client_document'),
    path('clients/<uuid:client_id>/popup/', views.client_popup, name='client_popup'),
    path('clients/<uuid:client_id>/report/', views.client_report, name='client_report'),
    path('clients/<uuid:client_id>/download-report/', views.client_download_report, name='client_download_report'),
    path('clients/ajax/', views.client_list_ajax, name='client_list_ajax'),
    
    # Portfolio Management
    path('portfolio/', portfolio_views.portfolio_dashboard, name='portfolio_dashboard'),
    path('portfolio/manager/<uuid:manager_id>/', portfolio_views.portfolio_manager_detail, name='portfolio_manager_detail'),
    path('portfolio/client/<uuid:client_id>/', client_portfolio_views.client_portfolio_detail, name='client_portfolio_detail'),
    path('portfolio/officer/<uuid:officer_id>/clients/', client_portfolio_views.officer_client_portfolio, name='officer_client_portfolio'),
    path('portfolio/assign/', portfolio_views.assign_clients, name='assign_clients'),
    path('portfolio/analytics/', portfolio_views.portfolio_analytics, name='portfolio_analytics'),
    path('portfolio/enhanced-analytics/', portfolio_views.enhanced_portfolio_dashboard, name='enhanced_portfolio_analytics'),
    path('portfolio/client-performance/', portfolio_views.client_performance_ranking, name='client_performance_ranking'),
    path('portfolio/benchmarking/', portfolio_views.portfolio_benchmarking_dashboard, name='portfolio_benchmarking_dashboard'),
    path('portfolio/reassign/<uuid:client_id>/', portfolio_views.reassign_client, name='reassign_client'),
    path('portfolio/bulk-assign/', portfolio_views.bulk_assign_clients, name='bulk_assign_clients'),
    path('api/portfolio/<uuid:manager_id>/performance/', portfolio_views.portfolio_performance_api, name='portfolio_performance_api'),
    path('api/portfolio/assignment-data/', portfolio_views.portfolio_assignment_api, name='portfolio_assignment_api'),
    path('api/manager-clients/<uuid:manager_id>/', portfolio_views.manager_clients_api, name='manager_clients_api'),
    
    # Client Analytics Dashboard
    path('analytics/client-growth/', client_analytics_views.client_analytics_dashboard, name='client_analytics_dashboard'),
    path('analytics/officer-performance/', client_analytics_views.officer_performance_dashboard, name='officer_performance_dashboard'),
    path('analytics/client-journey/<uuid:client_id>/', client_analytics_views.client_journey_mapping, name='client_journey_mapping'),
    path('analytics/export/', client_analytics_views.export_analytics_data, name='export_analytics_data'),
    
    # Client Analytics API endpoints
    path('api/analytics/acquisition-trends/', client_analytics_views.acquisition_trends_api, name='acquisition_trends_api'),
    path('api/analytics/demographic-analysis/', client_analytics_views.demographic_analysis_api, name='demographic_analysis_api'),
    path('api/analytics/lifecycle-analytics/', client_analytics_views.lifecycle_analytics_api, name='lifecycle_analytics_api'),
    path('api/analytics/officer-performance/', client_analytics_views.officer_performance_api, name='officer_performance_api'),
    
    # Growth Forecasting Dashboard
    path('forecasting/growth/', growth_forecasting_views.growth_forecasting_dashboard, name='growth_forecasting_dashboard'),
    path('forecasting/export/', growth_forecasting_views.export_forecast_report, name='export_forecast_report'),
    
    # Growth Forecasting API endpoints
    path('api/forecasting/predictive-models/', growth_forecasting_views.predictive_models_api, name='predictive_models_api'),
    path('api/forecasting/seasonal-analysis/', growth_forecasting_views.seasonal_analysis_api, name='seasonal_analysis_api'),
    path('api/forecasting/capacity-planning/', growth_forecasting_views.capacity_planning_api, name='capacity_planning_api'),
    path('api/forecasting/early-warning/', growth_forecasting_views.early_warning_api, name='early_warning_api'),
    
    # Audit and Security Reporting
    path('audit/dashboard/', audit_views.audit_dashboard, name='audit_dashboard'),
    path('audit/trail/', audit_views.audit_trail_report, name='audit_trail_report'),
    path('audit/activity/', audit_views.user_activity_analysis, name='user_activity_analysis'),
    path('audit/security/', audit_views.security_compliance_report, name='security_compliance_report'),
    path('audit/permissions/', audit_views.permission_usage_analytics, name='permission_usage_analytics'),
    path('audit/export/', audit_views.export_audit_report, name='export_audit_report'),
    path('audit/alerts/', audit_views.security_alerts, name='security_alerts'),
    path('audit/alerts/<uuid:alert_id>/resolve/', audit_views.resolve_alert, name='resolve_alert'),
    
    # API endpoints
    path('api/search-clients/', views.api_search_clients, name='api_search_clients'),
    
    # Branch management
    path('branches/', views.branch_list, name='branch_list'),
    path('branches/create/', views.branch_create, name='branch_create'),
    path('branches/<uuid:pk>/update/', views.branch_update, name='branch_update'),
    path('branches/<uuid:branch_id>/switch/', views.switch_branch, name='switch_branch'),
]