"""This module contains the views for the ``reveal`` app"""

from rest_framework.generics import RetrieveAPIView
from rest_framework.response import Response

from mtp.apps.trainings.models import TrainingSession

from .serializers import RevealBaseConfigSerializer, RevealPhotosSerializer


class RevealBaseConfigView(RetrieveAPIView):
    """View for the base configuration of the reveal"""

    lookup_url_kwarg = 'session_uuid'
    queryset = TrainingSession.objects.non_deleted()
    serializer_class = RevealBaseConfigSerializer


class PhotoListConfigView(RevealBaseConfigView):
    """View for photos configuration of the reveal"""

    serializer_class = RevealPhotosSerializer

    def get(self, request, *args, **kwargs):
        """Return a list of the session's users resized avatars

        For the parameters, see ``rest_framework.mixins.RetrieveModelMixin``

        """

        session = self.get_object()
        data = []
        # Users contains only trainees (I aggree, it's not well named...)
        for user in session.users.non_deleted().exclude(photo=None):
            data.append(self.serializer_class(user).data)
        # But we also want the trainer if he has a photo
        if session.trainer.photo:
            data.append(self.serializer_class(session.trainer).data)

        return Response(data)
