"""This module holds the v2 api-relataed views of the ``workshops`` app"""

from django.shortcuts import get_object_or_404

from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from mtp.apps.accounts.models import MTPUser
from mtp.apps.trainings.models import TrainingSession
from mtp.apps.workshops.models import (
    Workshop
)

from .serializers import (
    WorkshopSerializer,
    WorkshopTraineeDetailedScoreSerializer,
    WorkshopTraineeScoreSerializer,
)


class WorkshopViewSet(viewsets.ReadOnlyModelViewSet):
    """ViewSet for the ``Workshop`` model"""

    permission_classes = [IsAuthenticated]
    serializer_class = WorkshopSerializer
    queryset = Workshop.objects.non_deleted()


class WorkshopTraineeScoreView(APIView):
    """View to get the results of a given trainee for a given workshop"""

    permission_classes = [IsAuthenticated]

    def get(self, request, workshop_uuid, session_uuid, user_uuid):  # pylint: disable=unused-argument
        """Get the workshop trainee's scores for a given session

        Parameters
        ----------
        session_uuid: str
            The session uuid we want to get the workshop scores from
        workshop_uuid: str
            The workshop uuid we we want to get the scores for
        user_uuid: str
            The user uuid we want the scores for

        Examples
        --------
        {
            'score': 0,
            'average_score': 0.0,
            'maximum_score': 0
        }

        """

        training_session = get_object_or_404(
            TrainingSession.objects.non_deleted(),
            pk=session_uuid
        )
        workshop = get_object_or_404(
            Workshop.objects.non_deleted(),
            pk=workshop_uuid
        )
        trainee = get_object_or_404(
            MTPUser.objects.non_deleted().trainees(),
            pk=user_uuid
        )

        workshop_result_serializer = WorkshopTraineeScoreSerializer(
            workshop,
            context={
                'training_session': training_session,
                'trainee': trainee
            }
        )

        return Response(workshop_result_serializer.data)


class WorkshopTraineeDetailedScoreView(APIView):
    """View to get the results details of a given trainee for a given workshop"""

    permission_classes = [IsAuthenticated]

    def get(self, request, workshop_uuid, session_uuid, user_uuid):  # pylint: disable=unused-argument
        """Get the workshop trainee's scores for a given session

        Parameters
        ----------
        session_uuid: str
            The session uuid we want to get the workshop scores from
        workshop_uuid: str
            The workshop uuid we we want to get the scores for
        user_uuid: str
            The user uuid we want the scores for

        Examples
        --------
        {
            'id': 'f68aabc0-294c-4367-b303-70cee1898e36',
            'score': 0,
            'name': 'Quiz-0',
            'average_score': 0.0,
            'questions': [
                {
                    'id': 'ee95351e-a239-44c6-bc79-6a446ed74a47',
                    'title': 'Question-0',
                    'open_answer': None,
                    'is_single_choice': True,
                    'is_all_or_nothing': False,
                    'score': 0,
                    'average_score': 0.0,
                    'is_open': False,
                    'answers': [
                        {
                            'id': '11ed5bd0-b949-486e-9bbb-a08454639ae4',
                            'is_selected': False,
                            'label': '',
                            'is_correct': True,
                            'session_rate': 0
                        },
                        {
                            'id': '9285aecc-629c-4c72-a40a-5465ab11967c',
                            'is_selected': False,
                            'label': '',
                            'is_correct': False,
                            'session_rate': 0
                        }
                    ]
                },
                {
                    'answers': [],
                    'is_single_choice': True,
                    'is_all_or_nothing': False,
                    'score': 0,
                    'title': 'Question-2',
                    'open_answer': "User open answer",
                    'is_open': True,
                    'id': '85ce2781-8ecc-4ae1-83d5-ea6e4892285a',
                    'average_score': 0.0
                }
            ]
        }

        For the parameters, see ``rest_framework.views.APIView``

        """

        training_session = get_object_or_404(
            TrainingSession.objects.non_deleted(),
            pk=session_uuid
        )
        workshop = get_object_or_404(
            Workshop.objects.non_deleted(),
            pk=workshop_uuid
        )
        trainee = get_object_or_404(
            MTPUser.objects.non_deleted().trainees(),
            pk=user_uuid
        )

        workshop_result_serializer = WorkshopTraineeDetailedScoreSerializer(
            workshop,
            context={
                'training_session': training_session,
                'trainee': trainee
            }
        )

        return Response(workshop_result_serializer.data)
