"""
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.http import HttpRequest
from django.shortcuts import get_object_or_404
from django.urls import reverse

from compensation.forms.modals.document import NewEcoAccountDocumentModalForm
from compensation.models import EcoAccount, EcoAccountDocument
from konova.decorators import shared_access_required, default_group_required
from konova.forms.modals import EditDocumentModalForm
from konova.utils.documents import remove_document, get_document
from konova.utils.message_templates import DOCUMENT_EDITED, DOCUMENT_ADDED


@login_required
@default_group_required
@shared_access_required(EcoAccount, "id")
def new_document_view(request: HttpRequest, id: str):
    """ Renders a form for uploading new documents

    Args:
        request (HttpRequest): The incoming request
        id (str): The account's id to which the new document will be related
    Returns:

    """
    acc = get_object_or_404(EcoAccount, id=id)
    form = NewEcoAccountDocumentModalForm(request.POST or None, request.FILES or None, instance=acc, request=request)
    return form.process_request(
        request,
        msg_success=DOCUMENT_ADDED,
        redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data",
    )


@login_required
@default_group_required
@shared_access_required(EcoAccount, "id")
def get_document_view(request: HttpRequest, id:str, doc_id: str):
    """ Returns the document as downloadable file

    Wraps the generic document fetcher function from konova.utils.

    Args:
        request (HttpRequest): The incoming request
        id (str): The account id
        doc_id (str): The document id

    Returns:

    """
    acc = get_object_or_404(EcoAccount, id=id)
    doc = get_object_or_404(EcoAccountDocument, id=doc_id)
    return get_document(doc)


@login_required
@default_group_required
@shared_access_required(EcoAccount, "id")
def edit_document_view(request: HttpRequest, id: str, doc_id: str):
    """ Removes the document from the database and file system

    Wraps the generic functionality from konova.utils.

    Args:
        request (HttpRequest): The incoming request
        id (str): The account id
        doc_id (str): The document id

    Returns:

    """
    acc = get_object_or_404(EcoAccount, id=id)
    doc = get_object_or_404(EcoAccountDocument, id=doc_id)
    form = EditDocumentModalForm(request.POST or None, request.FILES or None, instance=acc, document=doc, request=request)
    return form.process_request(
        request,
        DOCUMENT_EDITED,
        reverse("compensation:acc:detail", args=(id,)) + "#related_data"
    )


@login_required
@default_group_required
@shared_access_required(EcoAccount, "id")
def remove_document_view(request: HttpRequest, id: str, doc_id: str):
    """ Removes the document from the database and file system

    Wraps the generic functionality from konova.utils.

    Args:
        request (HttpRequest): The incoming request
        id (str): The account id
        doc_id (str): The document id

    Returns:

    """
    acc = get_object_or_404(EcoAccount, id=id)
    doc = get_object_or_404(EcoAccountDocument, id=doc_id)
    return remove_document(
        request,
        doc
    )

