from rest_framework import viewsets
from ces_fall_rest_api.models.major_question import MajorQuestion
from ces_fall_rest_api.serializers.major_question_serializer import MajorQuestionSerializer
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import action
from rest_framework.response import Response


class MajorQuestionViewSet(viewsets.ModelViewSet):
    """A simple viewset to get the all the MajorQuestion objects
    This class has a token authentication.

    Args:
        viewsets (_type_): viewsets class
    """
    queryset = MajorQuestion.objects.all()
    serializer_class = MajorQuestionSerializer
    permission_classes = [IsAuthenticated]

    def get_object(self):
        lookup_args = ['pk', 'semester_id']

        if all(arg in self.kwargs for arg in lookup_args):
            return MajorQuestion.objects.get(major=self.kwargs['pk'], semester=self.kwargs['semester_id'])
        else:
            return super().get_object()

    @action(detail=False, methods=['get'], url_path='major_questions_per_semester_id')
    def major_questions_per_semester_id(self, request, id):
        queryset = MajorQuestion.objects.filter(semester=id)
        serializer = MajorQuestionSerializer(queryset, many=True)

        return Response(serializer.data)

    @action(detail=False, methods=['get'], url_path='major_questions_per_semester')
    def major_questions_per_semester(self, request, year, month, major):
        queryset = MajorQuestion.objects.get(
            semester__year=year, semester__month=month, major=major)
        return Response(self.get_serializer(queryset).data)
