from django.contrib.auth.models import User
from django.db import transaction
from apps.notifications.models import Notification

def send_notification_to_all(sender, message, notification_type):
    users = User.objects.exclude(id=sender.id)  # Excluir o remetente
    notifications = [
        Notification(sender=sender, user=user, message=message, notification_type=notification_type, send_to_all=True)
        for user in users
    ]
    Notification.objects.bulk_create(notifications)

def send_notification_to_user(sender, recipient, message, notification_type):
    Notification.objects.create(sender=sender, user=recipient, message=message, notification_type=notification_type)

def delete_sent_notification(notification_id, sender):
    notification = Notification.objects.filter(id=notification_id, sender=sender).first()
    if notification:
        notification.delete()
        return True
    return False
