from rest_framework import viewsets, status
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
from notifications.models import Notification
from notifications.api.serializers import NotificationSerializer


class NotificationViewSet(viewsets.ModelViewSet):
    serializer_class = NotificationSerializer
    permission_classes = [IsAuthenticated]
    
    def get_queryset(self):
        """
        Return notifications for the current user (must be admin/staff).
        """
        user = self.request.user
        if user.is_staff and user.is_superuser:
            return Notification.objects.filter(recipient=user)
        # Non-admin users don't see any notifications
        return Notification.objects.none()
    
    @action(detail=False, methods=['get'])
    def unread_count(self, request):
        """
        Get count of unread notifications for the current user.
        """
        unread_count = Notification.objects.filter(recipient=request.user, is_read=False).count()
        return Response({'unread_count': unread_count})
    
    @action(detail=False, methods=['get'])
    def unread(self, request):
        """
        Get all unread notifications for the current user.
        """
        notifications = Notification.objects.filter(recipient=request.user, is_read=False).order_by('-created_at')
        serializer = self.get_serializer(notifications, many=True)
        return Response(serializer.data)
    
    @action(detail=True, methods=['post'])
    def mark_as_read(self, request, pk=None):
        """
        Mark a specific notification as read.
        """
        notification = self.get_object()
        notification.mark_as_read()
        serializer = self.get_serializer(notification)
        return Response(serializer.data, status=status.HTTP_200_OK)
    
    @action(detail=True, methods=['post'])
    def mark_as_unread(self, request, pk=None):
        """
        Mark a specific notification as unread.
        """
        notification = self.get_object()
        notification.mark_as_unread()
        serializer = self.get_serializer(notification)
        return Response(serializer.data, status=status.HTTP_200_OK)
    
    @action(detail=False, methods=['post'])
    def mark_all_as_read(self, request):
        """
        Mark all notifications as read for the current user.
        """
        notifications = Notification.objects.filter(recipient=request.user, is_read=False)
        count = notifications.update(is_read=True)
        return Response(
            {'message': f'{count} notification(s) marked as read'},
            status=status.HTTP_200_OK
        )
    
    @action(detail=False, methods=['delete'])
    def clear_all(self, request):
        """
        Delete all notifications for the current user.
        """
        count, _ = Notification.objects.filter(recipient=request.user).delete()
        return Response(
            {'message': f'{count} notification(s) deleted'},
            status=status.HTTP_204_NO_CONTENT
        )
