"""
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.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpRequest, Http404
from django.shortcuts import get_object_or_404
from django.urls import reverse

from intervention.forms.modals.deduction import NewEcoAccountDeductionModalForm, RemoveEcoAccountDeductionModalForm, \
    EditEcoAccountDeductionModalForm
from intervention.models import Intervention
from konova.decorators import default_group_required, shared_access_required
from konova.utils.message_templates import DEDUCTION_ADDED, DEDUCTION_REMOVED, DEDUCTION_EDITED


@login_required
@default_group_required
@shared_access_required(Intervention, "id")
def new_deduction_view(request: HttpRequest, id: str):
    """ Renders a modal form view for creating deductions

    Args:
        request (HttpRequest): The incoming request
        id (str): The intervention's id which shall benefit from this deduction

    Returns:

    """
    intervention = get_object_or_404(Intervention, id=id)
    form = NewEcoAccountDeductionModalForm(request.POST or None, instance=intervention, request=request)
    return form.process_request(
        request,
        msg_success=DEDUCTION_ADDED,
        redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data",
    )


@login_required
@default_group_required
@shared_access_required(Intervention, "id")
def remove_deduction_view(request: HttpRequest, id: str, deduction_id: str):
    """ Renders a modal view for removing deductions

    Args:
        request (HttpRequest): The incoming request
        id (str): The intervention's id
        deduction_id (str): The deduction's id

    Returns:

    """
    intervention = get_object_or_404(Intervention, id=id)
    try:
        eco_deduction = intervention.deductions.get(id=deduction_id)
    except ObjectDoesNotExist:
        raise Http404("Unknown deduction")

    form = RemoveEcoAccountDeductionModalForm(request.POST or None, instance=intervention, deduction=eco_deduction, request=request)
    return form.process_request(
        request=request,
        msg_success=DEDUCTION_REMOVED,
        redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data"
    )


@login_required
@default_group_required
@shared_access_required(Intervention, "id")
def edit_deduction_view(request: HttpRequest, id: str, deduction_id: str):
    """ Renders a modal view for removing deductions

    Args:
        request (HttpRequest): The incoming request
        id (str): The intervention's id
        deduction_id (str): The deduction's id

    Returns:

    """
    intervention = get_object_or_404(Intervention, id=id)
    try:
        eco_deduction = intervention.deductions.get(id=deduction_id)
    except ObjectDoesNotExist:
        raise Http404("Unknown deduction")

    form = EditEcoAccountDeductionModalForm(request.POST or None, instance=intervention, deduction=eco_deduction, request=request)
    return form.process_request(
        request=request,
        msg_success=DEDUCTION_EDITED,
        redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data"
    )

