"""
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 compensation.models import EcoAccount
from intervention.forms.modals.deduction import EditEcoAccountDeductionModalForm, RemoveEcoAccountDeductionModalForm, \
    NewEcoAccountDeductionModalForm
from konova.decorators import default_group_required
from konova.utils.message_templates import DEDUCTION_EDITED, DEDUCTION_REMOVED, DEDUCTION_ADDED


@login_required
@default_group_required
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 eco account's id

    Returns:

    """
    acc = get_object_or_404(EcoAccount, id=id)
    if not acc.recorded:
        raise Http404()
    form = NewEcoAccountDeductionModalForm(request.POST or None, instance=acc, request=request)
    return form.process_request(
        request,
        msg_success=DEDUCTION_ADDED,
        redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
    )


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

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

    Returns:

    """
    acc = get_object_or_404(EcoAccount, id=id)
    try:
        eco_deduction = acc.deductions.get(id=deduction_id)
        if not eco_deduction.intervention.is_shared_with(request.user):
            raise ObjectDoesNotExist()
    except ObjectDoesNotExist:
        raise Http404("Unknown deduction")

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


@login_required
@default_group_required
def deduction_edit_view(request: HttpRequest, id: str, deduction_id: str):
    """ Renders a modal view for editing deductions

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

    Returns:

    """
    acc = get_object_or_404(EcoAccount, id=id)
    try:
        eco_deduction = acc.deductions.get(id=deduction_id)
        if not eco_deduction.intervention.is_shared_with(request.user):
            raise ObjectDoesNotExist
    except ObjectDoesNotExist:
        raise Http404("Unknown deduction")

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


