"""This module holds the managers and querysets for the ``brainstormers`` app."""

from django.db import models

from mtp.apps.sync.managers import SyncableQuerySet


class BrainstormerManager(models.Manager):
    """The Manager used by the ``Brainstormer`` model."""

    pass


class ProxyfiedBrainstormerManager(BrainstormerManager):
    """The Manager used by the ``PropositionWall``, ``QuestionWall`` and ``WordCloud`` model."""

    def get_queryset(self):
        """Filter only `Brainstormer` intances with `brainsormer_type` `PROPOSITIONWALL`.

        Returns
        -------
        Queryset
            ``mtp.apps.brainstormers.models.PropositionWall``

        """
        return super().get_queryset().filter(
            brainstormer_type=self.model.proxyfied_type)


class BrainstormerQuerySet(SyncableQuerySet, models.QuerySet):
    """The QuerySet used by the ``Brainstormer`` model."""

    def for_training_session(self, training_session):
        """ Filter only ``Brainstormer`` instances that belong to the given training session.

        Returns
        -------
        QuerySet of ``mtp.apps.brainstormers.models.Brainstormer``
        """
        return self.filter(session=training_session)


class BrainstormerPropositionGroupManager(models.Manager):
    """The Manager used by the ``BrainstormerPropositionGroup`` model."""
    pass


class BrainstormerPropositionGroupQuerySet(SyncableQuerySet, models.QuerySet):
    """The QuerySet used by the ``BrainstormerPropositionGroup`` model."""
    pass


class BrainstormerPropositionManager(models.Manager):
    """The Manager used by the ``BrainstormerProposition`` model."""
    pass

class BrainstormerPropositionQuerySet(SyncableQuerySet, models.QuerySet):
    """The QuerySet used by the ``BrainstormerProposition`` model."""

    def annotate_with_likes(self):
        """Annotate the queryset with likes.

        Returns
        -------
        Queryset
            ``mtp.apps.brainstormers.models.Brainstormer``
        """
        return self.annotate(
            models.Count("likes")).order_by("likes__count", "sort_order")
