"""This module holds the serializers for the ``ideas`` app"""

from rest_framework import serializers

from mtp.apps.likes.api.serializers import LikeableSerializer
from mtp.apps.trainings.apiv2.serializers import SessionActivitySerializer

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


class BaseIdeaSerializer(LikeableSerializer):
    """Base serializer for the subclasses of the ``Idea`` abstract model"""

    class Meta:
        fields = (
            'id',
            'text',
            'user',
            'session',
            'created',
        ) + LikeableSerializer.Meta.fields

        read_only_fields = (
            'created',
        )


class BrainstormingIdeaSerializer(BaseIdeaSerializer):
    """Serializer for the ``BrainstormingIdea`` model"""

    tags = serializers.PrimaryKeyRelatedField(
        many=True,
        queryset=BrainstormingTag.objects.non_deleted(),
        required=False
    )

    class Meta:
        model = BrainstormingIdea
        fields = BaseIdeaSerializer.Meta.fields + (
            'brainstorming',
            'tags',
        )
        read_only_fields = BaseIdeaSerializer.Meta.read_only_fields


class BrainstormingTagSerializer(serializers.ModelSerializer):
    """Serializer for the ``BrainstormingTag`` model"""

    class Meta:
        model = BrainstormingTag
        fields = (
            'id',
            'name',
            'color',
            'brainstorming',
            'session',
            'sort_order'
        )
        read_only_fields = (
            'sort_order',
        )
        # By default, DRF, even if `sort_order` is add in 'read_only_fields`,
        # will add this validator :
        # ```python
        # UniqueTogetherValidator(
        #     queryset=BrainstormingTag.objects.all(),
        #     fields=('sort_order', 'brainstorming', 'session',)
        # )
        # ```
        # But if we don't add `sort_order` in the serializer definition, the validator is not added.
        # So, as it is read only, and there is only this validator, we erase validators list.
        # http://www.django-rest-framework.org/api-guide/validators/#optional-fields
        validators = []


class BaseIdeasActivitySerializer(SessionActivitySerializer):
    """Base Serializer for the models inheriting of the ``IdeasActivity``"""

    class Meta:
        fields = SessionActivitySerializer.Meta.fields + (
            'is_anonymous',
        )
        read_only_fields = SessionActivitySerializer.Meta.read_only_fields
        validators = SessionActivitySerializer.Meta.validators


class BrainstormingSerializer(BaseIdeasActivitySerializer):
    """Serializer for the ``Brainstorming`` model"""

    class Meta:
        model = Brainstorming
        fields = BaseIdeasActivitySerializer.Meta.fields
        read_only_fields = BaseIdeasActivitySerializer.Meta.read_only_fields
        validators = BaseIdeasActivitySerializer.Meta.validators


class QuestionWallIdeaSerializer(BaseIdeaSerializer):
    """Serializer for the ``QuestionWallIdea`` model"""

    class Meta:
        model = QuestionWallIdea
        fields = BaseIdeaSerializer.Meta.fields + (
            'questionwall',
        )
        read_only_fields = BaseIdeaSerializer.Meta.read_only_fields


class QuestionWallSerializer(BaseIdeasActivitySerializer):
    """Serializer for the ``QuestionWall`` model"""

    class Meta:
        model = QuestionWall
        fields = BaseIdeasActivitySerializer.Meta.fields
        read_only_fields = BaseIdeasActivitySerializer.Meta.read_only_fields
        validators = BaseIdeasActivitySerializer.Meta.validators


class WordCloudIdeaSerializer(BaseIdeaSerializer):
    """Serializer for the ``WordCloudIdea`` model"""

    class Meta:
        model = WordCloudIdea
        fields = BaseIdeaSerializer.Meta.fields + (
            'wordcloud',
        )
        read_only_fields = BaseIdeaSerializer.Meta.read_only_fields


class WordCloudSerializer(BaseIdeasActivitySerializer):
    """Serializer for the ``WordCloud`` model"""

    class Meta:
        model = WordCloud
        fields = BaseIdeasActivitySerializer.Meta.fields
        read_only_fields = BaseIdeasActivitySerializer.Meta.read_only_fields
        validators = BaseIdeasActivitySerializer.Meta.validators

