from apps.calls.models import Notification


class NotificationRepository:

    @staticmethod
    def base_queryset():
        return (
            Notification.objects
            .select_related("recipient", "recipient__profile")
            .only(
                "id",
                "notification_type",
                "title",
                "message",
                "is_read",
                "read_at",
                "created_at",
                "recipient_id",
            )
        )

    @staticmethod
    def list_for_user(user):
        return (
            NotificationRepository.base_queryset()
            .filter(recipient=user)
            .order_by("-created_at")
        )

    @staticmethod
    def get_by_id(notification_id, user):
        return (
            NotificationRepository.base_queryset()
            .filter(id=notification_id, recipient=user)
            .first()
        )


    @staticmethod
    def mark_as_read(instance):
        if instance is None:
            return None
        instance.mark_as_read()
        return instance

    @staticmethod
    def create_notification(recipient, notification_type, title, message, call=None):
        """Create a new notification for a user"""
        return Notification.objects.create(
            recipient=recipient,
            notification_type=notification_type,
            title=title,
            message=message,
            call=call
        )
