from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

from .models import Notification
from .serializers import NotificationSerializer

from utils.helpers import verify_token


class NotificationsView(APIView):

    def get(self, request):

        user_id = verify_token(request)

        if not user_id:
            return Response(
                {
                    "message": "Unauthorized"
                },
                status=status.HTTP_401_UNAUTHORIZED
            )

        notifications = Notification.objects.filter(
            user_id=user_id
        ).order_by("-id")

        serializer = NotificationSerializer(
            notifications,
            many=True
        )

        unread_count = notifications.filter(
            is_read=False
        ).count()

        return Response(
            {
                "message": "Notifications fetched successfully",
                "count": notifications.count(),
                "unread_count": unread_count,
                "notifications": serializer.data,
                "status": 200
            }
        )
    


class MarkNotificationReadView(APIView):

    def post(self, request):

        user_id = verify_token(request)

        if not user_id:
            return Response(
                {
                    "message": "Unauthorized"
                },
                status=status.HTTP_401_UNAUTHORIZED
            )

        notification_id = request.data.get(
            "notification_id"
        )

        if not notification_id:
            return Response(
                {
                    "message": "notification_id is required"
                },
                status=status.HTTP_400_BAD_REQUEST
            )

        try:

            notification = Notification.objects.get(
                id=notification_id,
                user_id=user_id
            )

            notification.is_read = True
            notification.save()

            return Response(
                {
                    "message": "Notification marked as read",
                    "status": 200
                }
            )

        except Notification.DoesNotExist:

            return Response(
                {
                    "message": "Notification not found"
                },
                status=status.HTTP_404_NOT_FOUND
            )
        


class MarkAllNotificationsReadView(APIView):

    def post(self, request):

        user_id = verify_token(request)

        if not user_id:
            return Response(
                {
                    "message": "Unauthorized"
                },
                status=status.HTTP_401_UNAUTHORIZED
            )

        Notification.objects.filter(
            user_id=user_id,
            is_read=False
        ).update(
            is_read=True
        )

        return Response(
            {
                "message": "All notifications marked as read",
                "status": 200
            }
        )
    



class DeleteNotificationView(APIView):

    def post(self, request):

        user_id = verify_token(request)

        if not user_id:
            return Response(
                {
                    "message": "Unauthorized"
                },
                status=status.HTTP_401_UNAUTHORIZED
            )

        notification_id = request.data.get(
            "notification_id"
        )

        if not notification_id:
            return Response(
                {
                    "message": "notification_id is required"
                },
                status=status.HTTP_400_BAD_REQUEST
            )

        try:

            notification = Notification.objects.get(
                id=notification_id,
                user_id=user_id
            )

            notification.delete()

            return Response(
                {
                    "message": "Notification deleted successfully",
                    "status": 200
                }
            )

        except Notification.DoesNotExist:

            return Response(
                {
                    "message": "Notification not found"
                },
                status=status.HTTP_404_NOT_FOUND
            )
        


class NotificationCountView(APIView):

    def get(self, request):

        user_id = verify_token(request)

        if not user_id:
            return Response(
                {
                    "message": "Unauthorized"
                },
                status=status.HTTP_401_UNAUTHORIZED
            )

        unread_count = Notification.objects.filter(
            user_id=user_id,
            is_read=False
        ).count()

        return Response(
            {
                "unread_count": unread_count,
                "status": 200
            }
        )