from rest_framework import viewsets
from ces_fall_rest_api.models.user import User
from ces_fall_rest_api.serializers.user_serializer import UserSerializer
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.permissions import IsAdminUser, IsAuthenticated
from rest_framework.decorators import action


class UserViewSet(viewsets.ModelViewSet, APIView):
    """A simple viewset to get the all the User objects
    This class has a token authentication.

    Args:
        viewsets (_type_): viewset class
        APIView (_type_): APIView class

    Returns:
        _type_: Response in JSON format
    """
    queryset = User.objects.all()
    serializer_class = UserSerializer
    permission_classes = [IsAuthenticated | IsAdminUser]

    @action(detail=True, permission_classes=[IsAuthenticated])
    def logout(self, request, pk=None):
        self.get_object().auth_token.delete()
        return Response({f'{self.get_object().username} logged out'})
