import datetime

from django.contrib.auth.models import Group
from django.shortcuts import render
from oauth2_provider.contrib.rest_framework import TokenHasReadWriteScope
from notification.serializers import NotificationSerializer, DashboardNotificationSerializer
from rest_framework import viewsets, status
from rest_framework.response import Response
from notification.models import Notification, DashboardNotification


# Create your views here.
class NotificationViewSet(viewsets.ModelViewSet):
    queryset = Notification.objects.all()
    serializer_class = NotificationSerializer
    permission_classes = [TokenHasReadWriteScope]

    def list(self, request, *args, **kwargs):
        if "key" in request.GET:
            add_obj = Notification.objects.get(key__exact=request.GET["key"])
            serializer = self.serializer_class(add_obj)
            return Response(serializer.data, status=status.HTTP_200_OK)
        if "read_notif" in request.GET:
            add_obj = Notification.objects.filter(
                user_id=request.user.id, status="read"
            )
            query = [x for x in add_obj]
            serializer = self.serializer_class(query, many=True)
            return Response(serializer.data, status=status.HTTP_200_OK)
        if "unread_notif" in request.GET:
            add_obj = Notification.objects.filter(
                user_id=request.user.id, status="unread"
            )
            query = [x for x in add_obj]
            serializer = self.serializer_class(query, many=True)
            return Response(serializer.data, status=status.HTTP_200_OK)
        if "pending_notif" in request.GET:
            add_obj = Notification.objects.filter(
                user_id=request.user.id, status="pending"
            )
            query = [x for x in add_obj]
            serializer = self.serializer_class(query, many=True)
            return Response(serializer.data, status=status.HTTP_200_OK)
        else:
            queryset = Notification.objects.all()
            serializer = self.serializer_class(queryset, many=True)
            return Response(serializer.data, status=status.HTTP_200_OK)

    def create(self, request, *args, **kwargs):

        serializer = self.serializer_class(data=request.data)
        if serializer.is_valid():
            obj = serializer.create(validated_data=request.data)
            obj.save()

            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors)

    def retrieve(self, request, pk=None, *args, **kwargs):
        queryset = Notification.objects.get(key__exact=request.GET["key"])
        serializer = self.serializer_class(queryset)
        return Response(serializer.data, status=status.HTTP_200_OK)

    def update(self, request, pk=None, *args, **kwargs):
        queryset = Notification.objects.get(key__exact=request.GET["key"])

        queryset.save()
        serializer = self.serializer_class(queryset)
        serializer.update(instance=queryset, validated_data=request.data)
        return Response(serializer.data, status=status.HTTP_200_OK)

    def partial_update(self, request, pk=None, *args, **kwargs):
        pass

    def destroy(self, request, pk=None, *args, **kwargs):
        queryset = Notification.objects.get(key__exact=request.GET["key"])
        queryset.trash = True
        queryset.save()
        return Response(status=status.HTTP_200_OK)


class DashboardNotificationViewSet(viewsets.ModelViewSet):
    queryset = DashboardNotification.objects.all()
    serializer_class = DashboardNotificationSerializer
    permission_classes = [TokenHasReadWriteScope]

    def list(self, request, *args, **kwargs):
        role=request.GET.get('role')
        query=self.queryset.filter(role__name=role,trash=False).order_by('-date')
        ser_data=self.serializer_class(query,many=True).data
        return Response(ser_data,status=status.HTTP_200_OK)

    def create(self, request, *args, **kwargs):
        grop=Group.objects.filter(name__in=request.data['role'])
        request.data.pop('role')
        serializer = self.serializer_class(data=request.data)
        if serializer.is_valid():
            obj = serializer.create(validated_data=request.data)
            obj.date=datetime.datetime.now()
            obj.save()
            obj.role.set(grop)
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors)