"""
URL конфигурация для платформы.
"""
from django.contrib import admin
from django.urls import path, re_path, include
from django.conf import settings
from django.conf.urls.static import static
from rest_framework import permissions

from config.views import serve_media
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

# Swagger/OpenAPI schema
schema_view = get_schema_view(
    openapi.Info(
        title="Образовательная платформа API",
        default_version='v1',
        description="API документация для образовательной SaaS платформы",
        terms_of_service="https://www.platform.com/terms/",
        contact=openapi.Contact(email="contact@platform.com"),
        license=openapi.License(name="Proprietary"),
    ),
    public=True,
    permission_classes=(permissions.AllowAny,),
)

urlpatterns = [
    # Admin панель
    path('admin/', admin.site.urls),
    
    # API документация
    path('api/swagger<format>/', schema_view.without_ui(cache_timeout=0), name='schema-json'),
    path('api/swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    path('api/redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
    
    # Health check
    path('health/', include('config.health_urls')),
    
    # API endpoints
    path('api/', include('apps.users.urls')),
    path('api/schedule/', include('apps.schedule.urls')),
    path('api/notifications/', include('apps.notifications.urls')),
    path('api/video/', include('apps.video.urls')),
    path('api/board/', include('apps.board.urls')),
    path('api/homework/', include('apps.homework.urls')),
    path('api/materials/', include('apps.materials.urls')),
    path('api/subscriptions/', include('apps.subscriptions.urls')),
    path('api/analytics/', include('apps.analytics.urls')),
    path('api/chat/', include('apps.chat.urls')),
    path('api/', include('apps.referrals.urls')),
]

# Статика: WhiteNoise раздаёт при прямом обращении (8123); nginx — с порта 80
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
# Медиа: своя view serve_media (работает при DEBUG=False на 8123); nginx — с порта 80
urlpatterns += [
    re_path(r'^media/(?P<path>.*)$', serve_media),
]

if settings.DEBUG:
    # Django Debug Toolbar
    try:
        import debug_toolbar
        urlpatterns = [
            path('__debug__/', include(debug_toolbar.urls)),
        ] + urlpatterns
    except ImportError:
        pass
    
    # Django Silk (профилирование) — только если silk в INSTALLED_APPS
    if 'silk' in settings.INSTALLED_APPS:
        try:
            urlpatterns += [
                path('silk/', include('silk.urls', namespace='silk')),
            ]
        except ImportError:
            pass

# Кастомизация admin панели
admin.site.site_header = "Образовательная платформа - Администрирование"
admin.site.site_title = "Платформа Admin"
admin.site.index_title = "Добро пожаловать в панель администрирования"

