import { api } from '@/services/api'

export interface AppNotification {
  id: number
  type: string
  title: string
  message: string | null
  action_url: string | null
  read_at: string | null
  created_at: string | null
}

export interface BusinessAlert {
  id: string
  type: string
  category: string
  title: string
  message: string
  action_url: string
}

export interface NotificationPayload {
  summary: {
    unread_notifications: number
    active_alerts: number
    total: number
  }
  notifications: AppNotification[]
  alerts: BusinessAlert[]
}

export const notificationService = {
  async all() {
    const { data } = await api.get<{ data: NotificationPayload }>('/notifications')

    return data.data
  },

  async create(payload: {
    type?: string
    title: string
    message?: string
    action_url?: string
  }) {
    const { data } = await api.post<{ data: AppNotification }>('/notifications', payload)

    return data.data
  },

  async markRead(notification: AppNotification) {
    const { data } = await api.patch<{ data: AppNotification }>(`/notifications/${notification.id}/read`)

    return data.data
  },

  async markAllRead() {
    await api.post('/notifications/read-all')
  },
}
