from datetime import datetime

import requests
from django.conf import settings
from django.contrib.auth import authenticate
from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_simplejwt.authentication import JWTAuthentication
from module_project.utils import ApiResponse
from module_project import constants

from .serializers import InAppNotificationSerializer

from ..models import InAppNotification


class InAppNotificationCountAPIView(APIView):
    authentication_classes = [JWTAuthentication]
    permission_classes = [IsAuthenticated]
    model = InAppNotification

    def get(self, request, *args, **kwargs):
        count = InAppNotification.pending_read_count(user=request.user)
        return ApiResponse.success(message=constants.SUCCESS, data={"count": count})


class InAppNotificationListAPIView(APIView):
    authentication_classes = [JWTAuthentication]
    permission_classes = [IsAuthenticated]
    model = InAppNotification
    serializer_class = InAppNotificationSerializer

    def get(self, request, *args, **kwargs):
        obj = InAppNotification.latest_15(user=request.user)
        serializer_obj = self.serializer_class(obj, many=True)
        return ApiResponse.success(message=constants.SUCCESS, data=serializer_obj.data)


class InAppNotificationReadAPIView(APIView):
    authentication_classes = [JWTAuthentication]
    permission_classes = [IsAuthenticated]
    model = InAppNotification
    serializer_class = InAppNotificationSerializer

    def get(self, request, *args, **kwargs):
        obj = InAppNotification.objects.filter(user=request.user).update(is_read=True)
        return ApiResponse.success(message=constants.SUCCESS)