from allauth.account.decorators import secure_admin_login
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from django.urls.conf import include
from django.utils.translation import gettext_lazy as _

from servala.frontend import urls, views

admin.site.site_title = _("Servala Admin")
admin.site.site_header = _("Servala Management")
admin.site.index_title = _("Dashboard")
admin.site.login = secure_admin_login(admin.site.login)

urlpatterns = [
    path("", include((urls, "servala.frontend"), namespace="frontend")),
    # This adds the allauth urls to the project:
    # - accounts/keycloak/login/
    # - accounts/keycloak/login/callback/
    path("accounts/", include("allauth.urls")),
    path("admin/", admin.site.urls),
    path("api/", include("servala.api.urls")),
]

# Serve static and media files in development
if settings.DEBUG:  # pragma: no cover
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += [
        path(
            "error404/",
            views.custom_404,
            {"exception": Exception("Test 404")},
            name="error_404",
        ),
        path(
            "error403/",
            views.custom_403,
            {"exception": Exception("Test 403")},
            name="error_403",
        ),
        path("error500/", views.custom_500, name="error_500"),
    ]

# Custom error handlers
handler404 = "servala.frontend.views.custom_404"
handler403 = "servala.frontend.views.custom_403"
handler500 = "servala.frontend.views.custom_500"
