# -*- coding: utf-8 -*-
"""
Notifications URLs

This module defines URL patterns for the notifications app,
including both web views and API endpoints for managing
notifications, preferences, and templates.

URL Patterns:
    - Web Views: List, detail, preferences, stats
    - API Views: RESTful endpoints for notifications
    - AJAX Views: Real-time notification actions

Author: AdTlas Development Team
Version: 1.0.0
Last Updated: 2024
"""

from django.urls import path, include
from rest_framework.routers import DefaultRouter
from django.views.decorators.csrf import csrf_exempt

from . import views

# App namespace
app_name = 'notifications'

# API Router for ViewSets
router = DefaultRouter()
# Add ViewSets here if you create any

# Web URL patterns
web_urlpatterns = [
    # Notification List and Detail Views
    path(
        '',
        views.NotificationListView.as_view(),
        name='notification_list'
    ),
    path(
        '<int:pk>/',
        views.NotificationDetailView.as_view(),
        name='notification_detail'
    ),
    
    # User Preferences
    path(
        'preferences/',
        views.NotificationPreferencesView.as_view(),
        name='notification_preferences'
    ),
    
    # Statistics and Dashboard
    path(
        'stats/',
        views.NotificationStatsView.as_view(),
        name='notification_stats'
    ),
    path(
        'dashboard/',
        views.NotificationStatsView.as_view(),
        name='notification_dashboard'
    ),
    
    # Create and Bulk Operations
    path(
        'create/',
        views.NotificationCreateView.as_view(),
        name='notification_create'
    ),
    path(
        'bulk/',
        views.BulkNotificationView.as_view(),
        name='bulk_notification'
    ),
    
    # AJAX Actions
    path(
        'mark-read/<int:pk>/',
        views.NotificationMarkReadView.as_view(),
        name='notification_mark_read'
    ),
    path(
        'archive/<int:pk>/',
        views.NotificationArchiveView.as_view(),
        name='notification_archive'
    ),
    path(
        'bulk-action/',
        views.NotificationBulkActionView.as_view(),
        name='notification_bulk_action'
    ),
    
    # Export
    path(
        'export/',
        views.NotificationExportView.as_view(),
        name='notification_export'
    ),
]

# API URL patterns
api_urlpatterns = [
    # Notification CRUD
    path(
        '',
        views.NotificationAPIView.as_view(),
        name='api_notification_list'
    ),
    path(
        '<int:pk>/',
        views.NotificationDetailAPIView.as_view(),
        name='api_notification_detail'
    ),
    
    # Statistics
    path(
        'stats/',
        views.NotificationStatsAPIView.as_view(),
        name='api_notification_stats'
    ),
    
    # Preferences
    path(
        'preferences/',
        views.NotificationPreferencesAPIView.as_view(),
        name='api_notification_preferences'
    ),
    path(
        'preferences/<int:notification_type_id>/',
        views.NotificationPreferencesAPIView.as_view(),
        name='api_notification_preference_detail'
    ),
    
    # Quick Actions
    path(
        'mark-read/<int:pk>/',
        csrf_exempt(views.mark_notification_read_api),
        name='api_mark_notification_read'
    ),
    path(
        'mark-all-read/',
        csrf_exempt(views.mark_all_read_api),
        name='api_mark_all_read'
    ),
    path(
        'count/',
        views.notification_count_api,
        name='api_notification_count'
    ),
    
    # Bulk Operations
    path(
        'bulk-action/',
        views.NotificationBulkActionAPIView.as_view(),
        name='api_notification_bulk_action'
    ),
    path(
        'send/',
        csrf_exempt(views.send_notification_api),
        name='api_send_notification'
    ),
    path(
        'send-bulk/',
        views.BulkNotificationAPIView.as_view(),
        name='api_send_bulk_notification'
    ),
    
    # Templates
    path(
        'templates/',
        views.NotificationTemplateAPIView.as_view(),
        name='api_notification_templates'
    ),
    path(
        'templates/<int:pk>/',
        views.NotificationTemplateDetailAPIView.as_view(),
        name='api_notification_template_detail'
    ),
    
    # Types
    path(
        'types/',
        views.NotificationTypeAPIView.as_view(),
        name='api_notification_types'
    ),
    path(
        'types/<int:pk>/',
        views.NotificationTypeDetailAPIView.as_view(),
        name='api_notification_type_detail'
    ),
    
    # Queue Management
    path(
        'queue/',
        views.NotificationQueueAPIView.as_view(),
        name='api_notification_queue'
    ),
    path(
        'queue/<int:pk>/',
        views.NotificationQueueDetailAPIView.as_view(),
        name='api_notification_queue_detail'
    ),
    
    # History
    path(
        'history/',
        views.NotificationHistoryAPIView.as_view(),
        name='api_notification_history'
    ),
    
    # Real-time endpoints
    path(
        'realtime/unread/',
        views.UnreadNotificationsAPIView.as_view(),
        name='api_unread_notifications'
    ),
    path(
        'realtime/recent/',
        views.RecentNotificationsAPIView.as_view(),
        name='api_recent_notifications'
    ),
    
    # Export
    path(
        'export/',
        views.NotificationExportAPIView.as_view(),
        name='api_notification_export'
    ),
]

# WebSocket URL patterns (if using Django Channels)
websocket_urlpatterns = [
    # Real-time notification updates
    # path('ws/notifications/', views.NotificationConsumer.as_asgi()),
]

# Main URL patterns
urlpatterns = [
    # Web views
    path('', include(web_urlpatterns)),
    
    # API views
    path('api/', include(api_urlpatterns)),
    
    # Router URLs (for ViewSets)
    path('api/', include(router.urls)),
    
    # Admin-specific URLs
    path('admin/', include([
        path(
            'types/',
            views.NotificationTypeListView.as_view(),
            name='admin_notification_types'
        ),
        path(
            'types/create/',
            views.NotificationTypeCreateView.as_view(),
            name='admin_notification_type_create'
        ),
        path(
            'types/<int:pk>/edit/',
            views.NotificationTypeUpdateView.as_view(),
            name='admin_notification_type_edit'
        ),
        path(
            'templates/',
            views.NotificationTemplateListView.as_view(),
            name='admin_notification_templates'
        ),
        path(
            'templates/create/',
            views.NotificationTemplateCreateView.as_view(),
            name='admin_notification_template_create'
        ),
        path(
            'templates/<int:pk>/edit/',
            views.NotificationTemplateUpdateView.as_view(),
            name='admin_notification_template_edit'
        ),
        path(
            'queue/',
            views.NotificationQueueListView.as_view(),
            name='admin_notification_queue'
        ),
        path(
            'history/',
            views.NotificationHistoryListView.as_view(),
            name='admin_notification_history'
        ),
        path(
            'analytics/',
            views.NotificationAnalyticsView.as_view(),
            name='admin_notification_analytics'
        ),
    ])),
    
    # User-specific URLs
    path('user/', include([
        path(
            'unread/',
            views.UserUnreadNotificationsView.as_view(),
            name='user_unread_notifications'
        ),
        path(
            'archived/',
            views.UserArchivedNotificationsView.as_view(),
            name='user_archived_notifications'
        ),
        path(
            'settings/',
            views.UserNotificationSettingsView.as_view(),
            name='user_notification_settings'
        ),
    ])),
    
    # HTMX/AJAX endpoints
    path('htmx/', include([
        path(
            'notification-item/<int:pk>/',
            views.NotificationItemHTMXView.as_view(),
            name='htmx_notification_item'
        ),
        path(
            'notification-list/',
            views.NotificationListHTMXView.as_view(),
            name='htmx_notification_list'
        ),
        path(
            'unread-count/',
            views.UnreadCountHTMXView.as_view(),
            name='htmx_unread_count'
        ),
        path(
            'mark-read/<int:pk>/',
            views.MarkReadHTMXView.as_view(),
            name='htmx_mark_read'
        ),
        path(
            'archive/<int:pk>/',
            views.ArchiveHTMXView.as_view(),
            name='htmx_archive'
        ),
    ])),
]

# Additional URL patterns for specific notification types
# These can be used for type-specific handling
type_specific_urlpatterns = [
    path('security/', include([
        path(
            '',
            views.SecurityNotificationListView.as_view(),
            name='security_notifications'
        ),
        path(
            'settings/',
            views.SecurityNotificationSettingsView.as_view(),
            name='security_notification_settings'
        ),
    ])),
    
    path('system/', include([
        path(
            '',
            views.SystemNotificationListView.as_view(),
            name='system_notifications'
        ),
        path(
            'maintenance/',
            views.MaintenanceNotificationView.as_view(),
            name='maintenance_notifications'
        ),
    ])),
    
    path('marketing/', include([
        path(
            '',
            views.MarketingNotificationListView.as_view(),
            name='marketing_notifications'
        ),
        path(
            'campaigns/',
            views.MarketingCampaignNotificationView.as_view(),
            name='marketing_campaign_notifications'
        ),
    ])),
]

# Add type-specific patterns to main urlpatterns
urlpatterns += [
    path('types/', include(type_specific_urlpatterns)),
]

# URL patterns for testing (only in development)
if __debug__:
    test_urlpatterns = [
        path('test/', include([
            path(
                'send-test/',
                views.SendTestNotificationView.as_view(),
                name='test_send_notification'
            ),
            path(
                'bulk-test/',
                views.BulkTestNotificationView.as_view(),
                name='test_bulk_notification'
            ),
            path(
                'template-test/',
                views.TestTemplateView.as_view(),
                name='test_template'
            ),
        ])),
    ]
    urlpatterns += test_urlpatterns

# URL name mappings for reverse lookups
URL_NAMES = {
    # Web views
    'list': 'notifications:notification_list',
    'detail': 'notifications:notification_detail',
    'preferences': 'notifications:notification_preferences',
    'stats': 'notifications:notification_stats',
    'create': 'notifications:notification_create',
    'bulk': 'notifications:bulk_notification',
    
    # API views
    'api_list': 'notifications:api_notification_list',
    'api_detail': 'notifications:api_notification_detail',
    'api_stats': 'notifications:api_notification_stats',
    'api_preferences': 'notifications:api_notification_preferences',
    'api_count': 'notifications:api_notification_count',
    
    # Actions
    'mark_read': 'notifications:notification_mark_read',
    'archive': 'notifications:notification_archive',
    'bulk_action': 'notifications:notification_bulk_action',
    
    # Admin
    'admin_types': 'notifications:admin_notification_types',
    'admin_templates': 'notifications:admin_notification_templates',
    'admin_queue': 'notifications:admin_notification_queue',
    'admin_history': 'notifications:admin_notification_history',
    'admin_analytics': 'notifications:admin_notification_analytics',
}

# Export URL names for easy access
__all__ = ['urlpatterns', 'URL_NAMES', 'app_name']