"""This module holds the factories for the ``trainings`` app."""

import datetime

from django.utils import timezone

import factory
from factory.fuzzy import FuzzyChoice

from mtp.apps.accounts.factories import AdminFactory, TrainerFactory
from mtp.apps.tasks.factories import DjangoTaskFactory

from .models import (
    Training,
    TrainingActivity,
    TrainingActivityLibraryStatus,
    TrainingSession,
    TrainingTask
)

TODAY = timezone.now()
DATE_BEGIN = TODAY - datetime.timedelta(days=1)
DATE_END = DATE_BEGIN + datetime.timedelta(hours=4)


class TrainingFactory(factory.django.DjangoModelFactory):
    """Factory for the ``mtp.apps.trainings.models.Training`` model.

    Attributes
    ----------
    id : ``factory.Sequence``
    name : ``factory.Sequence``
        The resulting names are of the form "training-%d"
    owner : sub-factory of ``mtp.apps.accounts.factories.TrainerFactory``
        It's an admin per default but can be changed at creation time to be a trainer.

    Notes
    -----
    - The ``description`` field is left empty.

    """

    class Meta:
        model = Training

    name = factory.Sequence(lambda n: 'training-%d' % n)
    owner = factory.SubFactory(AdminFactory)


class TrainingSessionFactory(factory.django.DjangoModelFactory):
    """Factory for the ``mtp.apps.trainings.models.Training`` model.

    Attributes
    ----------
    id : ``factory.Sequence``
    training : sub-factory of ``TrainingFactory``
    trainer : sub-factory of ``mtp.apps.accounts.factories.TrainerFactory``

    Notes
    -----
    - The ``description`` field is left empty.

    """
    class Meta:
        model = TrainingSession

    training = factory.SubFactory(TrainingFactory)
    trainer = factory.SubFactory(TrainerFactory)
    start_date = DATE_BEGIN
    end_date = DATE_END


class TrainingActivityFactory(factory.django.DjangoModelFactory):
    """Factory for the ``mtp.apps.trainings.models.TrainingActivity`` model.

    Attributes
    ----------
    training : sub-factory of ``TrainingFactory``
    name : ``factory.Sequence``

    """

    class Meta:
        model = TrainingActivity

    training = factory.SubFactory(TrainingFactory)
    name = factory.Sequence(lambda n: 'training-activity-%d' % n)


class TrainingActivityLibraryStatusFactory(factory.django.DjangoModelFactory):
    """Factory for the ``TrainingActivityLibraryStatus`` model."""

    class Meta:
        model = TrainingActivityLibraryStatus


class TrainingTaskFactory(DjangoTaskFactory):
    """Factory for the ``mtp.apps.trainings.models.TrainingTask`` model.

    Attributes
    ----------
    training: TrainingFactory
        A training tied to this task
    task_type: str
        One value proposed by ``TASK_TYPES``

    """

    class Meta:
        model = TrainingTask

    training = factory.SubFactory(TrainingFactory)
    task_type = FuzzyChoice(TrainingTask.TASK_TYPES.values.keys())
