import requests

from django.conf import settings
from django.contrib.messages import get_messages
from django.db.models import Q
from django.http import HttpResponse
from rest_framework import exceptions, status, viewsets
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from lily.accounts.models import Account
from lily.contacts.models import Contact
from lily.tags.models import Tag

from .serializers import AddressSerializer, EmailAddressSerializer, PhoneNumberSerializer, TagSerializer
from ..models.models import Address, EmailAddress, PhoneNumber


class Notifications(APIView):
    """
    List all notifications posted in request.messages
    """

    def get(self, request, format=None, *args, **kwargs):
        storage = get_messages(request)
        notifications = []

        for message in storage:
            notifications.append({
                'level': message.level_tag,
                'message': message.message,
            })

        return Response(notifications)


class CallerName(APIView):
    """
    Serve a caller name to voipgrid based on the phone number provided
    """

    def get(self, request, format=None, *args, **kwargs):
        name = '[NK]'
        phone_number = request.GET.get('phonenumber', '')
        caller_name = request.GET.get('callername', '')

        if not phone_number:
            return HttpResponse()

        phone_number_end = phone_number[-9:]

        contact = Contact.objects.filter(
            Q(phone_numbers__raw_input__endswith=phone_number_end) |
            Q(phone_numbers__number__endswith=phone_number_end)
        ).filter(is_deleted=False).first()

        if contact:
            name = contact.full_name()
        else:
            account = Account.objects.filter(
                Q(phone_numbers__raw_input__endswith=phone_number_end) |
                Q(phone_numbers__number__endswith=phone_number_end)
            ).filter(is_deleted=False).first()

            if account:
                name = account.name
            else:
                name += caller_name

        return HttpResponse('status=ACK&callername=%s' % name, content_type='application/x-www-form-urlencoded')


class RelatedModelViewSet(viewsets.ModelViewSet):
    related_model = None

    def list(self, request, object_pk=None):
        queryset = self._get_related_queryset(object_pk).all()
        serializer = self.serializer_class(queryset, many=True)
        return Response(serializer.data)

    def retrieve(self, request, pk=None, object_pk=None):
        queryset = self._get_related_queryset(object_pk).filter(pk=pk)
        serializer = self.serializer_class(queryset, many=True)
        return Response(serializer.data)

    def create(self, request, object_pk=None):
        serializer = self.get_serializer(data=request.data, related_object=self._get_related_object(object_pk))
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

    def _get_related_object(self, object_pk):
        return self.related_model.objects.get(pk=object_pk)

    def _get_related_queryset(self, object_pk):
        pass


class PhoneNumberViewSet(RelatedModelViewSet):
    queryset = PhoneNumber.objects
    serializer_class = PhoneNumberSerializer

    def _get_related_queryset(self, object_pk):
        return self._get_related_object(object_pk).phone_numbers


class EmailAddressViewSet(RelatedModelViewSet):
    queryset = EmailAddress.objects
    serializer_class = EmailAddressSerializer

    def _get_related_queryset(self, object_pk):
        return self._get_related_object(object_pk).email_addresses


class AddressViewSet(RelatedModelViewSet):
    queryset = Address.objects
    serializer_class = AddressSerializer

    def _get_related_queryset(self, object_pk):
        return self._get_related_object(object_pk).addresses


class TagViewSet(RelatedModelViewSet):
    queryset = Tag.objects
    serializer_class = TagSerializer
    related_model = None

    def _get_related_queryset(self, object_pk):
        return self._get_related_object(object_pk).tags


class CountryViewSet(ModelViewSet):
    """
    List all countries for a tenant.
    """
    queryset = Address.objects
    allowed_methods = ['OPTIONS', 'POST']
    serializer_class = AddressSerializer

    def create(self, request, *args, **kwargs):
        raise exceptions.PermissionDenied

@@ -4498,32 +4498,230 @@
 ressSerializer%0A%0A
+    def get_queryset(self):%0A        %22%22%22%0A        Set the queryset here so it filters on tenant and works with pagination.%0A        %22%22%22%0A        return super(CountryViewSet, self).get_queryset().all()%0A%0A
     def create(s

Fix countries api endpoint for pagination