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

from django.db.models import Prefetch

from rest_framework import viewsets
from rest_framework_extensions.mixins import NestedViewSetMixin

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

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


class BrainstormerViewSet(NestedViewSetMixin, SoftDestroyModelMixin, viewsets.ModelViewSet):
    """
    A simple ViewSet for viewing and editing brainstormers.
    """

    queryset = Brainstormer.objects.non_deleted().prefetch_related(
        Prefetch(
            'groups',
            queryset=BrainstormerPropositionGroup.objects.non_deleted()
        ),
        Prefetch(
            'propositions',
            queryset=BrainstormerProposition.objects.non_deleted()
        ),
        Prefetch(
            'propositions__likes',
            queryset=Like.objects.non_deleted()
        )
    )
    serializer_class = BrainstormerSerializer


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


class BrainstormerPropositionViewSet(LikeableViewSet,
                                     NestedViewSetMixin,
                                     SoftDestroyModelMixin,
                                     viewsets.ModelViewSet):
    """
    A simple ViewSet for viewing and editing brainstormer propositions.
    """
    queryset = BrainstormerProposition.objects.non_deleted().prefetch_related(
        'likes'
    )
    serializer_class = BrainstormerPropositionSerializer


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