"""
Author: Michel Peltriaux
Created on: 14.12.25

"""
from abc import ABC

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

from konova.decorators import default_group_required
from konova.forms.modals import RemoveModalForm


class AbstractRemoveView(LoginRequiredMixin, View, ABC):
    _MODEL = None
    _REDIRECT_URL = None
    _FORM = RemoveModalForm

    def dispatch(self, request, *args, **kwargs):
        return super().dispatch(request, *args, **kwargs)

    @method_decorator(default_group_required)
    def __process_request(self, request: HttpRequest, id: str, *args, **kwargs) -> HttpResponse:
        obj = self._MODEL.objects.get(id=id)
        identifier = obj.identifier
        form = self._FORM(request.POST or None, instance=obj, request=request)
        return form.process_request(
            request,
            _("{} removed").format(identifier),
            redirect_url=reverse(self._REDIRECT_URL)
        )

    def get(self, request: HttpRequest, id: str, *args, **kwargs) -> HttpResponse:
        """ GET endpoint for removing via modal form

        Due to the legacy logic of the form (which processes get and post requests directly), we simply need to pipe
        the request from GET and POST endpoints directly into the same method.

        Args:
            request (HttpRequest): The incoming request
            id (str): The uuid id as string

        Returns:
        """
        return self.__process_request(request, id, *args, **kwargs)

    def post(self, request: HttpRequest, id: str, *args, **kwargs) -> HttpResponse:
        """  POST endpoint for removing via modal form

        Due to the legacy logic of the form (which processes get and post requests directly), we simply need to pipe
        the request from GET and POST endpoints directly into the same method.

        Args:
            request (HttpRequest): The incoming request
            id (str): The uuid id as string

        Returns:
        """
        return self.__process_request(request, id, *args, **kwargs)
