import request from '@/utils/http'

export interface InboxMessage {
  id: string
  recipient_kind: string
  recipient_user_id: string
  sender_type: string
  category: string
  title: string
  body: string
  link: string
  payload?: Record<string, unknown>
  read_at?: string
  created_at: string
}

export interface BroadcastInput {
  recipient_kind: 'admin' | 'portal'
  department_ids?: string[]
  category: string
  title: string
  body: string
  link?: string
}

export const fetchInbox = (scope = 'mine') =>
  request.get<InboxMessage[]>({ url: '/api/v1/admin/inbox', params: { scope } })
export const fetchInboxUnread = () => request.get<{ unread: number }>({ url: '/api/v1/admin/inbox/unread' })
export const markInboxRead = (id: string) =>
  request.post<{ read: boolean }>({ url: `/api/v1/admin/inbox/${id}/read` })
export const markInboxAllRead = () =>
  request.post<{ read_all: number }>({ url: '/api/v1/admin/inbox/read-all' })
export const broadcastInbox = (input: BroadcastInput) =>
  request.post<{ sent: number; ok: boolean }>({ url: '/api/v1/admin/inbox/broadcast', data: input })
