"""This module holds the test cases for the v2 api serializers of the ``workshops`` app"""

from mtp.apps.workshops.models import WorkshopStepQuestion
from mtp.apps.workshops.tests.test_cases import WorkshopTestCase

from ..serializers import (
    WorkshopAnswerTraineeScoreSerializer,
    WorkshopQuestionTraineeScoreSerializer,
    WorkshopTraineeDetailedScoreSerializer,
    WorkshopTraineeScoreSerializer,
)


class WorkshopTraineeScoreSerializerTestCase(WorkshopTestCase):
    """Test cases for the serializers of the workshop trainee's scores"""

    def test_workshop_answer_trainee_score_serializer(self):
        """Test that the ``WorkshopAnswerTraineeScoreSerializer`` is working correctly"""

        answer = self.workshop_question_mc.answers.first()

        serialized_answer = WorkshopAnswerTraineeScoreSerializer(
            answer,
            context={
                'trainee': self.trainee,
                'training_session': self.training_session
            }
        )

        self.assertEqual(
            serialized_answer.data,
            {
                'id': answer.id,
                'label': answer.label,
                'is_correct': answer.is_correct,
                'is_selected': answer.is_selected(
                    self.trainee,
                    self.training_session
                ),
                'session_rate': 0
            }
        )

    def test_workshop_question_trainee_score_serializer(self):
        """Test that the ``WorkshopQuestionTraineeScoreSerializer`` is working correctly"""

        question = self.workshop_open_question

        serialized_question = WorkshopQuestionTraineeScoreSerializer(
            question,
            context={
                'trainee': self.trainee,
                'training_session': self.training_session
            }
        )

        self.assertEqual(
            serialized_question.data,
            {
                'id': question.id,
                'title': question.title,
                'score': question.get_trainee_session_score(
                    self.trainee,
                    self.training_session
                ),
                'average_score': 0,
                'max_points_yield': question.max_points_yield,
                'answers': [],
                'open_answer': None,
                'is_open': question.is_open,
                'is_single_choice': question.is_single_choice,
                'is_all_or_nothing': question.is_all_or_nothing
            }
        )

    def test_workshop_trainee_score_serializer(self):
        """Test that the ``WorkshopTraineeScoreSerializer`` is working correctly"""

        serialized_score = WorkshopTraineeScoreSerializer(
            self.workshop,
            context={
                'trainee': self.trainee,
                'training_session': self.training_session
            }
        )

        self.assertEqual(
            serialized_score.data,
            {
                'score': self.workshop.get_trainee_session_score(
                    self.training_session,
                    self.trainee
                ),
                'average_score': self.workshop.get_training_session_average_score(
                    self.training_session
                ),
                'maximum_score': self.workshop.max_points_yield
            }
        )

    def test_workshop_trainee_detailed_score_serializer(self):
        """Test that the ``WorkshopTraineeDetailedScoreSerializer`` is working correctly"""

        context = {
            'trainee': self.trainee,
            'training_session': self.training_session
        }

        serialized_detailed_score = WorkshopTraineeDetailedScoreSerializer(
            self.workshop,
            context=context
        )

        self.assertEqual(
            serialized_detailed_score.data,
            {
                'id': self.workshop.id,
                'name': self.workshop.name,
                'score': self.workshop.get_trainee_session_score(
                    self.training_session,
                    self.trainee
                ),
                'average_score': self.workshop.get_training_session_average_score(
                    self.training_session
                ),
                'questions': [
                    WorkshopQuestionTraineeScoreSerializer(part, context=context).data
                    for part in self.workshop.steps.instance_of(WorkshopStepQuestion).non_deleted()
                ]
            }
        )

