"""This module holds the api-related views for the ``ideas`` app"""

from django.db.models import Prefetch

from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated
from rest_framework_extensions.mixins import NestedViewSetMixin

from mtp.apps.api.mixins import ListLiveModelMixin, SoftDestroyModelMixin
from mtp.apps.accounts.apiv2.permissions import IsTrainerToCreate, IsTrainerToEdit
from mtp.apps.likes.api.viewsets import LikeableViewSet

from ..models import (
    Brainstorming,
    BrainstormingIdea,
    BrainstormingTag,
    QuestionWall,
    QuestionWallIdea,
    WordCloud,
    WordCloudIdea,
)

from .serializers import (
    BrainstormingIdeaSerializer,
    BrainstormingSerializer,
    BrainstormingTagSerializer,
    QuestionWallIdeaSerializer,
    QuestionWallSerializer,
    WordCloudIdeaSerializer,
    WordCloudSerializer,
)


class BrainstormingViewSet(NestedViewSetMixin, SoftDestroyModelMixin, viewsets.ModelViewSet):
    """A simple ViewSet for viewing and editing ``Brainstorming`` instances"""

    permission_classes = (IsAuthenticated, IsTrainerToCreate, IsTrainerToEdit,)
    queryset = Brainstorming.objects.non_deleted()
    serializer_class = BrainstormingSerializer


class BrainstormingIdeaViewSet(LikeableViewSet,
                               NestedViewSetMixin,
                               SoftDestroyModelMixin,
                               ListLiveModelMixin,
                               viewsets.ModelViewSet):
    """A simple ViewSet for viewing and editing ``BrainstormingIdea`` instances"""

    permission_classes = (IsAuthenticated, IsTrainerToEdit)
    serializer_class = BrainstormingIdeaSerializer

    def get_queryset(self):
        """Don't prefetch if we are in the update case

        Returns
        -------
        BrainstormingIdeaQuerySet

        See Also
        --------
        https://github.com/encode/django-rest-framework/issues/2442

        """

        queryset = BrainstormingIdea.objects.non_deleted().filter(
            brainstorming_id=self.get_parents_query_dict()['brainstorming']
        )

        if self.request.method in ['POST', 'PATCH']:
            return queryset
        else:
            return queryset.prefetch_related(
                Prefetch(
                    'tags',
                    queryset=BrainstormingTag.objects.non_deleted()
                )
            )


class BrainstormingTagViewSet(NestedViewSetMixin,
                              SoftDestroyModelMixin,
                              ListLiveModelMixin,
                              viewsets.ModelViewSet):
    """A simple ViewSet for viewing and editing ``BrainstormingIdea`` instances"""

    permission_classes = (IsAuthenticated, IsTrainerToCreate, IsTrainerToEdit,)
    queryset = BrainstormingTag.objects.non_deleted()
    serializer_class = BrainstormingTagSerializer


class QuestionWallViewSet(NestedViewSetMixin, SoftDestroyModelMixin, viewsets.ModelViewSet):
    """A simple ViewSet for viewing and editing ``QuestionWall`` instances"""

    permission_classes = (IsAuthenticated, IsTrainerToCreate, IsTrainerToEdit,)
    queryset = QuestionWall.objects.non_deleted()
    serializer_class = QuestionWallSerializer


class QuestionWallIdeaViewSet(LikeableViewSet,
                              NestedViewSetMixin,
                              SoftDestroyModelMixin,
                              ListLiveModelMixin,
                              viewsets.ModelViewSet):
    """A simple ViewSet for viewing and editing ``QuestionWallIdea`` instances"""

    permission_classes = (IsAuthenticated, IsTrainerToEdit)
    queryset = QuestionWallIdea.objects.non_deleted()
    serializer_class = QuestionWallIdeaSerializer


class WordCloudViewSet(NestedViewSetMixin, SoftDestroyModelMixin, viewsets.ModelViewSet):
    """A simple ViewSet for viewing and editing ``WordCloud`` instances"""

    permission_classes = (IsAuthenticated, IsTrainerToCreate, IsTrainerToEdit,)
    queryset = WordCloud.objects.non_deleted()
    serializer_class = WordCloudSerializer


class WordCloudIdeaViewSet(LikeableViewSet,
                           NestedViewSetMixin,
                           SoftDestroyModelMixin,
                           ListLiveModelMixin,
                           viewsets.ModelViewSet):
    """A simple ViewSet for viewing and editing ``WordCloudIdea`` instances"""

    permission_classes = (IsAuthenticated, IsTrainerToEdit)
    queryset = WordCloudIdea.objects.non_deleted()
    serializer_class = WordCloudIdeaSerializer
