"""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,
    WorkshopSerializer,
    WorkshopStepImageSerializer,
    WorkshopStepQuestionSerializer,
    WorkshopStepQuestionAnswerSerializer,
    WorkshopTraineeDetailedScoreSerializer,
    WorkshopTraineeScoreSerializer,
)


class WorkshopSerializerTestCase(WorkshopTestCase):
    """Test cases for the ``WorkshopSerializer``"""

    def test_workshop_serializer_is_working(self):
        """Test that the ``Workshop`` serializer is working correctly"""

        serialized_workshop = WorkshopSerializer(self.workshop)

        self.assertEqual(
            serialized_workshop.data,
            {
                'id': self.workshop.id,
                'name': self.workshop.name,
                'description': self.workshop.description,
                'activity_type': self.workshop.activity_type,
                'sort_order': self.workshop.sort_order,
                'session': self.workshop.session_id,
                'training': self.workshop.training_id,
                'enable_score': self.workshop.enable_score,
                'steps': [
                    step.serializer_class(step).data
                    for step in self.workshop.steps.non_deleted()
                ]
            }
        )

    def test_workshop_step_question_is_working(self):
        """Test that the ``WorkshopStepQuestion`` serializer is working correctly"""

        serialized_question = WorkshopStepQuestionSerializer(self.workshop_question)

        self.assertEqual(
            serialized_question.data,
            {
                'id': self.workshop_question.id,
                'sort_order': self.workshop_question.sort_order,
                'step_name': self.workshop_question.step_name,
                'title': self.workshop_question.title,
                'description': self.workshop_question.description,
                'is_multiple_choices': self.workshop_question.is_multiple_choices,
                'is_all_or_nothing': self.workshop_question.is_all_or_nothing,
                'answers': [
                    WorkshopStepQuestionAnswerSerializer(answer).data
                    for answer in self.workshop_question.answers.non_deleted()
                ]
            }
        )

    def test_workshop_step_question_answer_serializer_is_working(self):
        """Test that the ``WorkshopStepQuestionAnswer`` serializer is working correctly"""

        answer = self.workshop_question.answers.first()
        serialized_answer = WorkshopStepQuestionAnswerSerializer(answer)

        self.assertEqual(
            serialized_answer.data,
            {
                'id': answer.id,
                'label': answer.label,
                'is_correct': answer.is_correct,
                'points': answer.points,
                'color': answer.color
            }
        )

    def test_workshop_step_image_serializer_is_working(self):
        """Test that the ``WorkshopStepImage`` serializer is working correctly"""

        serialized_image = WorkshopStepImageSerializer(
            self.workshop_question_mc_image
        )

        self.assertEqual(
            serialized_image.data,
            {
                'id': self.workshop_question_mc_image.id,
                'sort_order': self.workshop_question_mc_image.sort_order,
                'step_name': self.workshop_question_mc_image.step_name,
                'title': self.workshop_question_mc_image.title,
                'description': self.workshop_question_mc_image.description,
                'image': self.workshop_question_mc_image.image.url
            }
        )


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()
                ]
            }
        )

