"""Base test cases for testing workshops."""

from mtp.apps.common.tests.test_cases import MTPTestCase

from ..factories import (
    WorkshopFactory,
    WorkshopStepImageFactory,
    WorkshopStepQuestionFactory,
    WorkshopStepQuestionAnswerFactory,
    WorkshopStepQuestionUserAnswerFactory
)


class BaseWorkshopTestCase:
    """Base test case to tests workshops.

    It provides some default test data useful in a lot of tests.

    """

    def setUp(self):
        """Reset the score of the trainee for the workshop"""

        super().setUp()

        sorted_set = self.workshop.redis_instance.scores_for_session(
            self.training_session.pk
        )
        sorted_set.zremrangebyrank(0, -1)

    @classmethod
    def setUpTestData(cls):
        """Create some data to use in workshops tests."""

        super().setUpTestData()

        with cls.training.override_version_state(cls.training.VERSION_STATES.DRAFT):

            cls.workshop = WorkshopFactory(training=cls.training)

            # 1 "standard" question, with 2 answers, 1 valid and 1 invalid
            cls.workshop_question = WorkshopStepQuestionFactory(workshop=cls.workshop)
            WorkshopStepQuestionAnswerFactory(
                question=cls.workshop_question,
                is_correct=True,
                points=1
            )
            WorkshopStepQuestionAnswerFactory(
                question=cls.workshop_question,
                is_correct=False,
                points=0
            )

            # 1 "multiple choices" question with a step image and the ``is_all_or_nothing_flag`` on.
            # 2 valid answer, 1 invalid one
            cls.workshop_question_mc_image = WorkshopStepImageFactory(workshop=cls.workshop)
            cls.workshop_question_mc = WorkshopStepQuestionFactory(
                workshop=cls.workshop, is_multiple_choices=True, is_all_or_nothing=True)

            cls.answers = []
            cls.answers.extend(
                WorkshopStepQuestionAnswerFactory.create_batch(
                    2,
                    question=cls.workshop_question_mc,
                    is_correct=True,
                    points=1
                )
            )
            cls.answers.append(
                WorkshopStepQuestionAnswerFactory(
                    question=cls.workshop_question_mc,
                    is_correct=False,
                    points=0
                )
            )

            # 1 "open question" (a question without any predefined answer
            cls.workshop_open_question = WorkshopStepQuestionFactory(workshop=cls.workshop)

    def add_user_answers(self, answers):
        """Add new user answers for the current workshop."""

        for answer in answers:
            WorkshopStepQuestionUserAnswerFactory(
                question=answer.question,
                answer=answer,
                user=self.trainee,
                training_session=self.training_session
            )


class WorkshopTestCase(BaseWorkshopTestCase, MTPTestCase):
    """A test case based on ``BaseWorkshopTestCase`` and ``MTPTestCase``.

    Should be used in most cases.

    """
