from __future__ import annotations

from typing import TYPE_CHECKING

from django.contrib.auth.decorators import permission_required
from django.shortcuts import get_object_or_404
from django.urls import reverse

from django_spire.contrib.generic_views import portal_views
from django_spire.core.table.enums import ResponsiveMode
from django_spire.contrib.session.controller import SessionController


from ${module_path} import models
from ${module_path}.forms import ${model_class_name}ListFilterForm
from ${module_path}.constants import LIST_FILTERING_SESSION_KEY

if TYPE_CHECKING:
    from django.core.handlers.wsgi import WSGIRequest
    from django.template.response import TemplateResponse


@permission_required('${permission_prefix}.view_${model_instance_name}')
def ${detail_view_name}(request: WSGIRequest, pk: int) -> TemplateResponse:
    ${context_single_var} = get_object_or_404(models.${model_class_name}, pk=pk)

    context_data = {
        '${context_single_var}': ${context_single_var},
    }

    return portal_views.detail_view(
        request,
        obj=${context_single_var},
        context_data=context_data,
        template='${template_directory_path}/page/${detail_page_template_name}.html'
    )


@permission_required('${permission_prefix}.view_${model_instance_name}')
def ${list_view_name}(request: WSGIRequest) -> TemplateResponse:
    ${context_plural_var} = models.${model_class_name}.objects.process_session_filter(
        request=request,
        session_key=LIST_FILTERING_SESSION_KEY,
        form_class=${model_class_name}ListFilterForm,
    )

    context_data = {
        '${context_plural_var}': ${context_plural_var},
        'filter_session': SessionController(request, LIST_FILTERING_SESSION_KEY),
    }

    return portal_views.list_view(
        request,
        model=models.${model_class_name},
        context_data=context_data,
        template='${template_directory_path}/page/${list_page_template_name}.html'
    )
