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

from django.db.models import Prefetch

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

from mtp.apps.likes.api.viewsets import LikeableViewSet
from mtp.apps.likes.models import Like

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):  # pylint: disable=unused-argument
        """
        Destroy method should not delete instance but archive it.

        For the parameters, see ``rest_framework.mixins.DestroyMixin``
        """
        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().prefetch_related(
        Prefetch(
            'groups',
            queryset=BrainstormerPropositionGroup.objects.non_deleted()
        ),
        Prefetch(
            'groups__propositions',
            queryset=BrainstormerProposition.objects.non_deleted()
        ),
        Prefetch(
            'groups__propositions__likes',
            queryset=Like.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(LikeableViewSet,
                                     NestedViewSetMixin,
                                     BaseArchiveModelViewSet):
    """
    A simple ViewSet for viewing and editing brainstormer propositions.
    """
    queryset = BrainstormerProposition.objects.non_deleted().prefetch_related(
        'likes'
    )
    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
