"""This module holds the recommendation QuerySets and Managers for the ``assessments`` app."""

import logging

from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from django.db import models

from mtp.apps.sync.managers import SyncableQuerySet


logger = logging.getLogger('mtp.apps.assessments')


class RecommendationLevelManager(models.Manager):
    pass


class RecommendationLevelQuerySet(SyncableQuerySet, models.QuerySet):
    """
    The QuerySet for the
    ``mtp.apps.assessments.models.RecommendationLevel`` model

    """
    pass


class CurriculumManager(models.Manager):
    pass


class CurriculumQuerySet(SyncableQuerySet, models.QuerySet):
    """The QuerySet for the ``mtp.apps.assessments.models.Curriculum`` model"""

    def get_for_session(self, session):
        """Get the curriculum associated to this training session.

        The curriculum is found with the ``job_profile`` and ``business_unit`` attributes
        of the training associated with the given session.

        Parameters
        ----------
        session : ``mtp.apps.trainings.TrainingSession`` instance

        Returns
        -------
        instance of ``mtp.apps.assessments.models.recommendations.Curriculum``
            or None if one cannot be fetched.

        """

        curriculum = self.get_for_groups(
            session.training.job_profile,
            session.training.business_unit
        )

        if curriculum:
            curriculum._session = session

        return curriculum

    def get_for_groups(self, job_profile, business_unit=None):
        """Get the curriculum associated to this job_profile and this business_unit.

        Parameters
        ----------
        job_profile: Group
            The job_profile used to find the related curriculum.
        business_unit: Group
            The business_unit used to find the related curriculum. Optional.

        Returns
        -------
        Curriculum
            or None if one cannot be fetched.

        """

        try:
            curriculum = self.get(job_profile=job_profile, business_unit=business_unit)
        except (ObjectDoesNotExist, MultipleObjectsReturned):
            try:
                curriculum = self.get(job_profile=job_profile, business_unit=None)
            except (ObjectDoesNotExist, MultipleObjectsReturned):
                logger.exception(
                    "No curriculum for job profile %s", job_profile
                )
                return None

        return curriculum


class TopicManager(models.Manager):
    pass


class TopicQuerySet(SyncableQuerySet, models.QuerySet):
    """
    The QuerySet for the
    ``mtp.apps.assessments.models.Topic`` model

    """
    pass


class LessonManager(models.Manager):
    pass


class LessonQuerySet(SyncableQuerySet, models.QuerySet):
    """
    The QuerySet for the
    ``mtp.apps.assessments.models.Lesson`` model

    """
    pass


class LessonDimensionWeightManager(models.Manager):
    pass


class LessonDimensionWeightQuerySet(SyncableQuerySet, models.QuerySet):
    """
    The QuerySet for the
    ``mtp.apps.assessments.models.LessonDimensionWeight`` model

    """
    pass


class LessonRecommendationThresholdManager(models.Manager):
    pass


class LessonRecommendationThresholdQuerySet(SyncableQuerySet, models.QuerySet):
    """
    The QuerySet for the
    ``mtp.apps.assessments.models.LessonRecommendationThreshold`` model

    """
    pass


class UserLessonRecommendationManager(models.Manager):
    pass


class UserLessonRecommendationQuerySet(SyncableQuerySet, models.QuerySet):
    """
    The QuerySet for the
    ``mtp.apps.assessments.models.UserLessonRecommendation`` model

    """
    pass
