import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { Notification } from '@/types'
import { mockNotifications } from '@/mock'

export const useNotificationStore = defineStore('notification', () => {
  // State
  const notifications = ref<Notification[]>([...mockNotifications])
  const filterType = ref<string>('all')

  // Getters
  const filteredNotifications = computed(() => {
    if (filterType.value === 'all') return notifications.value
    return notifications.value.filter(n => n.type === filterType.value)
  })

  const unreadCount = computed(() => notifications.value.filter(n => !n.read).length)
  const taskUnreadCount = computed(() => notifications.value.filter(n => n.type === 'task' && !n.read).length)

  // Actions
  function setFilter(type: string) {
    filterType.value = type
  }

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

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

  return {
    notifications,
    filterType,
    filteredNotifications,
    unreadCount,
    taskUnreadCount,
    setFilter,
    markAsRead,
    markAllAsRead,
  }
})
