"""
URL Configuration for Stream Processor Django Project

This module defines the main URL routing configuration for the entire
Django application. It includes routes for the admin interface, API endpoints,
and web interface views.

The URL patterns are organized to provide:
- Admin interface for managing application data
- RESTful API endpoints for programmatic access
- Web interface for monitoring and control
- Health check endpoint for deployment monitoring
"""

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.http import JsonResponse
from django.views.decorators.cache import never_cache
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView, SpectacularRedocView


def health_check(request):
    """
    Simple health check endpoint for monitoring application status.
    
    This view provides a basic health check that can be used by load balancers,
    monitoring systems, and deployment tools to verify that the application
    is running and responding to requests.
    
    Args:
        request: The HTTP request object
        
    Returns:
        JsonResponse: A JSON response indicating the application status
    """
    return JsonResponse({
        'status': 'healthy',
        'message': 'Stream Processor is running'
    })


# Main URL patterns for the application
urlpatterns = [
    # Django admin interface
    # Provides web-based interface for managing application data
    path('admin/', admin.site.urls),
    
    # Health check endpoint
    # Used by monitoring systems and load balancers
    path('health/', never_cache(health_check), name='health_check'),
    
    # Enhanced Authentication URLs
    # Custom authentication system with 2FA, session management, and security features
    path('auth/', include('apps.authentication.urls')),
    
    # Fallback Django auth URLs for password reset (if needed)
    path('accounts/', include('django.contrib.auth.urls')),
    
    # Stream management web interface
    path('streams/', include('apps.streams.urls')),
    # Jingle and ad break management interface
    path('jingles/', include('apps.jingles.urls')),
    # Notification settings interface
    path('notifications/', include('apps.notifications.urls')),
    
    # Main dashboard and monitoring views (must be last due to empty path)
    path('', include('apps.monitoring.urls')),


    # API Documentation endpoints
    # OpenAPI schema and interactive documentation
    path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
    path('api/docs/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
    path('api/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'),
    
    # API endpoints
    # RESTful API for programmatic access to application functionality
    path('api/v1/', include([
        # Stream management API endpoints
        path('streams/', include('apps.streams.api.urls')),
        # Jingle detection and ad break API endpoints
        path('jingles/', include('apps.jingles.api.urls')),
        # Notification management API endpoints
        path('notifications/', include('apps.notifications.api.urls')),
        # Monitoring and metrics API endpoints
        path('monitoring/', include('apps.monitoring.api.urls')),
    ])),

]

# Serve media files during development
# In production, these should be served by a web server like Nginx
if settings.DEBUG:
    urlpatterns += static(
        settings.MEDIA_URL, 
        document_root=settings.MEDIA_ROOT
    )
    urlpatterns += static(
        settings.STATIC_URL, 
        document_root=settings.STATIC_ROOT
    )

# Configure admin site headers and titles
# Customize the Django admin interface appearance
admin.site.site_header = 'Stream Processor Administration'
admin.site.site_title = 'Stream Processor Admin'
admin.site.index_title = 'Welcome to Stream Processor Administration'
