from math import perm
from rest_framework import generics, permissions, status
from rest_framework.response import Response
from rest_framework.views import APIView

from .exceptions import NotYourProfile, ProfileNotFound
from .models import Profile
from .renderers import ProfileJSONRenderer
from .serializers import ProfileSerializer, UpdateProfileSerializer


class AgentListAPIView(generics.ListAPIView):
  permission_classes = [permissions.IsAuthenticated]
  queryset = Profile.objects.filter(is_agent=True)
  serializer_class = ProfileSerializer

'''
  from rest_framework import api_view, permissions

  @api_view(['GET'])
  @permission_classes((permissions.IsAuthenticated))
  def get_all_agents(req):
    agents = Profile.objects.filter(is_agent=True)
    serializer = ProfileSerializer(agent)
    name_spaced_response={'agents': serializer.data}
    return Response(name_spaced_response, status=status.HTTP_200_OK)
'''
class TopAgentsListAPIView(generics.ListAPIView):
  permission_classes = [permissions.IsAuthenticated]
  queryset = Profile.objects.filter(top_agent=True)
  serializer_class = ProfileSerializer

class GetProfileAPIView(APIView):
  permission_classes = [permissions.IsAuthenticated]
  renderer_classes = [ProfileJSONRenderer]
  
  def get(self, req):
    user = self.request.user
    user_profile = Profile.objects.get(user=user)
    serializer = ProfileSerializer(user_profile, context={'req': req})
    return Response(serializer.data, status=status.HTTP_200_OK)

class UpdateProfileAPIView(APIView):
  permission_classes = [permissions.IsAuthenticated]
  renderer_classes = [ProfileJSONRenderer]
  
  serializer_class = UpdateProfileSerializer

  def patch(self, req, username):
    try:
      Profile.objects.get(user__username=username)
    except Profile.DoesNotExist: 
      raise ProfileNotFound
    
    user_name = req.user.username
    if user_name != username:
      raise NotYourProfile
    
    data = req.data
    serializer = UpdateProfileSerializer(
      instance=req.user.profile, data=data, partial=True)

    serializer.is_valid(raise_exception=True)
    serializer.save()
    return Response(serializer.data, status=status.HTTP_200_OK)

from math import perm
from rest_framework import generics, permissions, status
from rest_framework.response import Response
from rest_framework.views import APIView

from .exceptions import NotYourProfile, ProfileNotFound
from .models import Profile
from .renderers import ProfileJSONRenderer
from .serializers import ProfileSerializer, UpdateProfileSerializer


class AgentListAPIView(generics.ListAPIView):
  permission_classes = [permissions.IsAuthenticated]
  queryset = Profile.objects.filter(is_agent=True)
  serializer_class = ProfileSerializer

'''
  from rest_framework import api_view, permissions

  @api_view(['GET'])
  @permission_classes((permissions.IsAuthenticated))
  def get_all_agents(req):
    agents = Profile.objects.filter(is_agent=True)
    serializer = ProfileSerializer(agent)
    name_spaced_response={'agents': serializer.data}
    return Response(name_spaced_response, status=status.HTTP_200_OK)
'''
class TopAgentsListAPIView(generics.ListAPIView):
  permission_classes = [permissions.IsAuthenticated]
  queryset = Profile.objects.filter(top_agent=True)
  serializer_class = ProfileSerializer

class GetProfileAPIView(APIView):
  permission_classes = [permissions.IsAuthenticated]
  renderer_classes = [ProfileJSONRenderer]
  
  def get(self, req):
    user = self.request.user
    user_profile = Profile.objects.get(user=user)
    serializer = ProfileSerializer(user_profile, context={'req': req})
    return Response(serializer.data, status=status.HTTP_200_OK)

class UpdateProfileAPIView(APIView):
  permission_classes = [permissions.IsAuthenticated]
  renderer_classes = [ProfileJSONRenderer]
  
  serializer_class = UpdateProfileSerializer

  def patch(self, req, username):
    try:
      Profile.objects.get(user__username=username)
    except Profile.DoesNotExist: 
      raise ProfileNotFound
    
    user_name = req.user.username
    if user_name != username:
      raise NotYourProfile
    
    data = req.data
    serializer = UpdateProfileSerializer(
      instance=req.user.profile, data=data, partial=True)

    serializer.is_valid(raise_exception=True)
    serializer.save()
    return Response(serializer.data, status=status.HTTP_200_OK)

from rest_framework import api_view, permissions    @api_view(['GET'])   @permission_classes((permissions.IsAuthenticated))   def get_all_agents(req):     agents = Profile.objects.filter(is_agent=True)     serializer = ProfileSerializer(agent)     name_spaced_response={'agents': serializer.data}     return Response(name_spaced_response, status=status.HTTP_200_OK)