"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: ksp-servicestelle@sgdnord.rlp.de
Created on: 19.08.22

"""
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpRequest, HttpResponse
from django.shortcuts import get_object_or_404
from django.utils.decorators import method_decorator
from django.utils.translation import gettext_lazy as _
from django.views import View

from intervention.forms.modals.check import CheckModalForm
from intervention.models import Intervention
from konova.decorators import registration_office_group_required, shared_access_required
from konova.utils.message_templates import INTERVENTION_INVALID

class InterventionCheckView(LoginRequiredMixin, View):

    def __process_request(self, request: HttpRequest, id: str, *args, **kwargs) -> HttpResponse:
        """ Renders check form for an intervention

        Args:
            request (HttpRequest): The incoming request
            id (str): Intervention's id

        Returns:

        """
        intervention = get_object_or_404(Intervention, id=id)
        form = CheckModalForm(request.POST or None, instance=intervention, request=request)
        return form.process_request(
            request,
            msg_success=_("Check performed"),
            msg_error=INTERVENTION_INVALID
        )

    @method_decorator(registration_office_group_required)
    @method_decorator(shared_access_required(Intervention, "id"))
    def get(self, request: HttpRequest, id: str, *args, **kwargs) -> HttpResponse:
        return self.__process_request(request, id, *args, **kwargs)

    @method_decorator(registration_office_group_required)
    @method_decorator(shared_access_required(Intervention, "id"))
    def post(self, request: HttpRequest, id: str, *args, **kwargs) -> HttpResponse:
        return self.__process_request(request, id, *args, **kwargs)
