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

import logging

from django.db import models

from mtp.apps.sync.managers import SyncableQuerySet


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


class CertificationProgramManager(models.Manager):
    """Manager used by the ``CertificationProgram`` model."""

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

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

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

        """
        from mtp.apps.assessments.models import CertificationProgram

        job_profile = session.training.job_profile
        try:
            program = self.get(tree__job_profile=job_profile)
        except (CertificationProgram.DoesNotExist,
                CertificationProgram.MultipleObjectsReturned):
            logger.exception(
                "No certification program for job profile %s", job_profile
            )
            return None

        return program


class CertificationProgramQuerySet(SyncableQuerySet, models.QuerySet):
    """QuerySet used by the ``CertificationProgram`` model."""
    pass


class CertificationStatusManager(models.Manager):
    """Manager used by the ``CertificationStatus`` model."""


class CertificationStatusQuerySet(SyncableQuerySet, models.QuerySet):
    """QuerySet used by the ``CertificationStatus`` model."""


class CertificationCompetencyThresholdManager(models.Manager):
    """Manager used by the ``CertificationCompetencyThreshold`` model."""
    pass


class CertificationCompetencyThresholdQuerySet(SyncableQuerySet, models.QuerySet):
    """QuerySet used by the ``CertificationCompetencyThreshold`` model."""
    pass


class CertificationProgramUserStatusManager(models.Manager):
    """Manager used by the ``CertificationProgramUserStatus`` model."""
    pass


class CertificationProgramUserStatusQuerySet(SyncableQuerySet, models.QuerySet):
    """QuerySet used by the ``CertificationProgramUserStatus`` model."""
    pass
