from django.utils.translation import gettext as _
from django.contrib.auth.models import User

from rest_framework import permissions, status
from rest_framework import viewsets
from rest_framework.response import Response

from ..serializers.ProfileSerializer import ProfileSerializer
from ..models.ProfileModel import ProfileModel


class ProfileViewSet(viewsets.ModelViewSet):
    queryset = ProfileModel.objects.all()
    serializer_class = ProfileSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

    def retrieve(self, request, username=None):
        query = User.objects.filter(username=username)
        if (not query):
            return Response({"detail": _("Profile not found.")}, status.HTTP_404_NOT_FOUND)
        query = self.get_queryset().filter(pk=query[0].pk)
        if (not query):
            return Response({"detail": _("Profile not found.")}, status.HTTP_404_NOT_FOUND)
        instance = query[0]
        return Response(self.serializer_class(instance).data)

    def retrieve_id(self, request, pk=None):
        query = self.get_queryset().filter(pk=pk)
        if (not query):
            return Response({"detail": _("Profile not found.")}, status.HTTP_404_NOT_FOUND)
        instance = query[0]
        return Response(self.serializer_class(instance).data)

    def list(self, request):
        serializer = ProfileSerializer(self.get_queryset(), many=True)
        return Response(serializer.data)
