"""
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 django.utils.translation import gettext_lazy as _

from intervention.forms.modals.document import NewInterventionDocumentModalForm
from intervention.models import Intervention, InterventionDocument
from konova.decorators import default_group_required, shared_access_required
from konova.forms.modals import EditDocumentModalForm
from konova.utils.documents import get_document, remove_document
from konova.utils.message_templates import DOCUMENT_ADDED, DOCUMENT_EDITED


@login_required
@default_group_required
@shared_access_required(Intervention, "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 intervention's id to which the new document will be related
    Returns:

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


@login_required
@default_group_required
@shared_access_required(Intervention, "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 intervention id
        doc_id (str): The document id

    Returns:

    """
    intervention = get_object_or_404(Intervention, id=id)
    doc = get_object_or_404(InterventionDocument, id=doc_id)
    return get_document(doc)


@login_required
@default_group_required
@shared_access_required(Intervention, "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 intervention id
        doc_id (str): The document id

    Returns:

    """
    intervention = get_object_or_404(Intervention, id=id)
    doc = get_object_or_404(InterventionDocument, id=doc_id)
    return remove_document(
        request,
        doc
    )


@login_required
@default_group_required
@shared_access_required(Intervention, "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 intervention id
        doc_id (str): The document id

    Returns:

    """
    intervention = get_object_or_404(Intervention, id=id)
    doc = get_object_or_404(InterventionDocument, id=doc_id)
    form = EditDocumentModalForm(request.POST or None, request.FILES or None, instance=intervention, document=doc, request=request)
    return form.process_request(
        request,
        DOCUMENT_EDITED,
        redirect_url=reverse("intervention:detail", args=(intervention.id,)) + "#related_data"
    )

