from django.conf import settings
from django.conf.urls import handler400, handler404, handler500
from django.conf.urls.static import static
from django.contrib import admin
from django.shortcuts import render
from django.urls import path, include
from django.contrib.sitemaps.views import sitemap
from django.http import HttpResponse
from django.views.generic.base import RedirectView


from hub.services.views.errors import bad_request, page_not_found, server_error
from .sitemaps import (
    StaticSitemap,
    ServiceSitemap,
    OfferingSitemap,
    CloudProviderSitemap,
    ConsultingPartnerSitemap,
)


def robots_txt(request):
    content = """User-agent: *
Allow: /
Sitemap: https://servala.com/sitemap.xml
"""
    return HttpResponse(content, content_type="text/plain")


handler400 = "hub.services.views.errors.bad_request"
handler404 = "hub.services.views.errors.page_not_found"
handler500 = "hub.services.views.errors.server_error"

sitemaps = {
    "static": StaticSitemap,
    "services": ServiceSitemap,
    "offerings": OfferingSitemap,
    "providers": CloudProviderSitemap,
    "partners": ConsultingPartnerSitemap,
}

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", include("hub.services.urls")),
    path("broker/", include("hub.broker.urls", namespace="broker")),
    path(
        "sitemap.xml",
        sitemap,
        {"sitemaps": sitemaps},
        name="django.contrib.sitemaps.views.sitemap",
    ),
    path("robots.txt", robots_txt, name="robots_txt"),
    path(
        "favicon.ico",
        RedirectView.as_view(url=settings.STATIC_URL + "img/favicon.ico"),
        name="favicon",
    ),
]
if settings.DEBUG:
    urlpatterns += [
        path("__reload__/", include("django_browser_reload.urls")),
        path("schema-viewer/", include("schema_viewer.urls")),
        path("test-400/", lambda request: render(request, "400.html"), name="test_400"),
        path("test-404/", lambda request: render(request, "404.html"), name="test_404"),
        path("test-500/", lambda request: render(request, "500.html"), name="test_500"),
    ]
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
