"""This module holds the api-related views for the brainstormers app"""

from rest_framework import status, viewsets
from rest_framework.response import Response
from rest_framework_extensions.mixins import NestedViewSetMixin

from ..models import (
    Brainstormer, BrainstormerActivity, BrainstormerProposition,
    BrainstormerPropositionGroup, BrainstormerTemplate,
)
from .serializers import (
    BrainstormerActivitySerializer, BrainstormerSerializer,
    BrainstormerPropositionGroupSerializer, BrainstormerPropositionSerializer,
    BrainstormerTemplateSerializer,
)


class BaseArchiveModelViewSet(viewsets.ModelViewSet):
    """
    Model ViewSet for retrieving, updating or archiving a model instance.
    """

    def destroy(self, request, *args, **kwargs):
        """
        Destroy method should not delete instance but archive it.
        """
        instance = self.get_object()
        instance.is_deleted = True
        instance.save()
        return Response(status=status.HTTP_204_NO_CONTENT)


class BrainstormerViewSet(NestedViewSetMixin, BaseArchiveModelViewSet):
    """
    A simple ViewSet for viewing and editing brainstormers.
    """
    queryset = Brainstormer.objects.non_deleted()
    serializer_class = BrainstormerSerializer


class BrainstormerTemplateViewSet(NestedViewSetMixin, BaseArchiveModelViewSet):
    """
    A simple ViewSet for viewing and editing brainstormer templates.
    """
    queryset = BrainstormerTemplate.objects.non_deleted()
    serializer_class = BrainstormerTemplateSerializer


class BrainstormerPropositionViewSet(NestedViewSetMixin, BaseArchiveModelViewSet):
    """
    A simple ViewSet for viewing and editing brainstormer propositions.
    """
    queryset = BrainstormerProposition.objects.non_deleted()
    serializer_class = BrainstormerPropositionSerializer


class BrainstormerPropositionGroupViewSet(NestedViewSetMixin, BaseArchiveModelViewSet):
    """
    A simple ViewSet for viewing and editing brainstormer groups.
    """
    queryset = BrainstormerPropositionGroup.objects.non_deleted()
    serializer_class = BrainstormerPropositionGroupSerializer


class BrainstormerActivityViewSet(NestedViewSetMixin, BaseArchiveModelViewSet):
    """
    A simple ViewSet for viewing and editing brainstormer activities.
    """
    queryset = BrainstormerActivity.objects.non_deleted()
    serializer_class = BrainstormerActivitySerializer
