import { ref } from 'vue'

const notifications = ref([])

export function useNotifications() {
  function addNotification(notification) {
    const id = Date.now()
    notifications.value.push({
      id,
      ...notification,
      createdAt: new Date(),
      read: false,
    })
    return id
  }

  function removeNotification(id) {
    const index = notifications.value.findIndex(n => n.id === id)
    if (index > -1) {
      notifications.value.splice(index, 1)
    }
  }

  function markAsRead(id) {
    const notification = notifications.value.find(n => n.id === id)
    if (notification) {
      notification.read = true
    }
  }

  function markAllAsRead() {
    notifications.value.forEach(n => {
      n.read = true
    })
  }

  function clearAll() {
    notifications.value = []
  }

  const unreadCount = () => notifications.value.filter(n => !n.read).length

  return {
    notifications,
    addNotification,
    removeNotification,
    markAsRead,
    markAllAsRead,
    clearAll,
    unreadCount,
  }
}
