from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer

from incident.models import Incident


def get_incident_count() -> int:
    """Return incidents count without resolved and false alarm status."""
    return Incident.objects.exclude(status__in=[Incident.Status.RESOLVED, Incident.Status.FALSE_ALARM]).count()


def incident_count_notification_to_ws():
    """Notification about incident count to websocket."""
    count = get_incident_count()
    channel_layer = get_channel_layer()
    async_to_sync(
        channel_layer.group_send
    )('notification', {'type': 'notification', 'data': {'incident_count': str(count)}})
