"""
Root URL configuration for Online Course Platform.
================================================
- Registers all app URL namespaces.
- Wires up the custom 404 / 500 error handlers (used when DEBUG=False).
- Serves media files in development only (in production, Apache serves them).
"""
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.views.generic import TemplateView

from apps.courses.models import Course
from apps.courses.verify_views import PublicCertificateVerifyView
from apps.certifications.models import CertificationProgram


class HomeView(TemplateView):
    """Public home page — published courses and certifications."""
    template_name = 'home.html'

    def get_context_data(self, **kwargs):
        from apps.courses.models import CATEGORY_BOTH, CATEGORY_CERT, CATEGORY_COURSE, Category
        context = super().get_context_data(**kwargs)
        cat = self.request.GET.get('category', '').strip()
        courses = Course.objects.filter(status='published').select_related('category')
        certs = CertificationProgram.objects.filter(is_published=True).select_related('category')
        if cat:
            courses = courses.filter(category__slug=cat)
            certs = certs.filter(category__slug=cat)
        context['courses'] = courses
        context['featured_courses'] = courses.filter(is_featured=True)[:6]
        context['certifications'] = certs
        context['categories'] = Category.objects.filter(is_active=True)
        context['selected_category'] = cat
        context['course_categories'] = Category.objects.filter(
            is_active=True, applies_to__in=[CATEGORY_COURSE, CATEGORY_BOTH]
        )
        context['cert_categories'] = Category.objects.filter(
            is_active=True, applies_to__in=[CATEGORY_CERT, CATEGORY_BOTH]
        )
        return context


urlpatterns = [
    # Django admin
    path('admin/', admin.site.urls),

    # First-time web installer (CodeCanyon-style)
    path('install/', include('apps.installer.urls', namespace='installer')),

    # Public certificate verification (Part 6) — printed on every PDF & badge
    path(
        'verify/<str:certificate_id>/',
        PublicCertificateVerifyView.as_view(),
        name='verify_certificate',
    ),

    # Public home page
    path('', HomeView.as_view(), name='home'),

    # App routes (each app has its own urls.py with a namespace)
    path('accounts/',    include('apps.accounts.urls',    namespace='accounts')),
    path('courses/',     include('apps.courses.urls',     namespace='courses')),
    path('enrollments/', include('apps.enrollments.urls', namespace='enrollments')),
    path('dashboard/',   include('apps.dashboard.urls',   namespace='dashboard')),
    path('support/',     include('apps.support.urls',     namespace='support')),
    path('certifications/', include('apps.certifications.urls', namespace='certifications')),
    path('promotions/', include('apps.promotions.urls', namespace='promotions')),
    path('', include('apps.mentorship.urls', namespace='mentorship')),
]

# ---------------------------------------------------------------------------
# Custom error handlers
# These are only active when DEBUG = False.
# Django automatically uses 404.html and 500.html from the templates/ folder.
# ---------------------------------------------------------------------------
handler404 = 'config.views.custom_404'
handler500 = 'config.views.custom_500'

# ---------------------------------------------------------------------------
# Serve media files.
# Development: always. Production / cPanel: when SERVE_MEDIA=True (default).
# Static files are handled by WhiteNoise after `collectstatic`.
# ---------------------------------------------------------------------------
if settings.DEBUG or getattr(settings, 'SERVE_MEDIA', True):
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

