from django.conf import settings
from django.conf.urls import include
from django.urls import path, re_path, reverse
from django.views.generic.base import RedirectView
from django.views.static import serve
from drf_yasg import openapi
from drf_yasg.views import get_schema_view

from extras.plugins.urls import plugin_admin_patterns, plugin_patterns, plugin_api_patterns
from netbox.views import APIRootView, HomeView, StaticMediaFailureView, SearchView
from users.views import LoginView, LogoutView
from .admin import admin_site


# TODO: Remove in v2.9
class RQRedirectView(RedirectView):
    """
    Temporary 301 redirect from the old URL to the new one.
    """
    permanent = True

    def get_redirect_url(self, *args, **kwargs):
        return reverse('rq_home')


openapi_info = openapi.Info(
    title="NetBox API",
    default_version='v2',
    description="API to access NetBox",
    terms_of_service="https://github.com/netbox-community/netbox",
    license=openapi.License(name="Apache v2 License"),
)

schema_view = get_schema_view(
    openapi_info,
    validators=['flex', 'ssv'],
    public=True,
)

_patterns = [

    # Base views
    path('', HomeView.as_view(), name='home'),
    path('search/', SearchView.as_view(), name='search'),

    # Login/logout
    path('login/', LoginView.as_view(), name='login'),
    path('logout/', LogoutView.as_view(), name='logout'),

    # Apps
    path('circuits/', include('circuits.urls')),
    path('dcim/', include('dcim.urls')),
    path('extras/', include('extras.urls')),
    path('ipam/', include('ipam.urls')),
    path('secrets/', include('secrets.urls')),
    path('tenancy/', include('tenancy.urls')),
    path('user/', include('users.urls')),
    path('virtualization/', include('virtualization.urls')),

    # API
    path('api/', APIRootView.as_view(), name='api-root'),
    path('api/circuits/', include('circuits.api.urls')),
    path('api/dcim/', include('dcim.api.urls')),
    path('api/extras/', include('extras.api.urls')),
    path('api/ipam/', include('ipam.api.urls')),
    path('api/secrets/', include('secrets.api.urls')),
    path('api/tenancy/', include('tenancy.api.urls')),
    path('api/virtualization/', include('virtualization.api.urls')),
    path('api/docs/', schema_view.with_ui('swagger'), name='api_docs'),
    path('api/redoc/', schema_view.with_ui('redoc'), name='api_redocs'),
    re_path(r'^api/swagger(?P<format>.json|.yaml)$', schema_view.without_ui(), name='schema_swagger'),

    # Serving static media in Django to pipe it through LoginRequiredMiddleware
    path('media/<path:path>', serve, {'document_root': settings.MEDIA_ROOT}),

    # Admin
    path('admin/', admin_site.urls),
    path('admin/background-tasks/', include('django_rq.urls')),
    # TODO: Remove in v2.9
    path('admin/webhook-backend-status/', RQRedirectView.as_view()),

    # Errors
    path('media-failure/', StaticMediaFailureView.as_view(), name='media_failure'),

    # Plugins
    path('plugins/', include((plugin_patterns, 'plugins'))),
    path('api/plugins/', include((plugin_api_patterns, 'plugins-api'))),
    path('admin/plugins/', include(plugin_admin_patterns))
]


if settings.DEBUG:
    import debug_toolbar
    _patterns += [
        path('__debug__/', include(debug_toolbar.urls)),
    ]

if settings.METRICS_ENABLED:
    _patterns += [
        path('', include('django_prometheus.urls')),
    ]

# Prepend BASE_PATH
urlpatterns = [
    path('{}'.format(settings.BASE_PATH), include(_patterns))
]

handler500 = 'utilities.views.server_error'
