from ces_fall_rest_api.models.gen_question import GenQuestion
from ces_fall_rest_api.serializers.gen_question_serializer import GenQuestionSerializer
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import action
from rest_framework.response import Response


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

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

    @action(detail=False, url_path='genquestions_by_semester')
    def genquestions_by_semester(self, request, year, month):
        questions_set = self.get_queryset().filter(
            semester__year=year, semester__month=month)
        return Response(GenQuestionSerializer(questions_set, many=True).data)

    @action(detail=False, methods=['get'], url_path='gen_question_per_semester')
    def gen_question_per_semester(self, request, id):
        queryset = GenQuestion.objects.filter(semester=id)
        serializer = GenQuestionSerializer(queryset, many=True)
        return Response(serializer.data)
