import { api } from '@/core/services/api'
import type {
  ListNotificationsParams,
  NotificationActionApiResponse,
  NotificationCountApiResponse,
  NotificationsApiResponse,
} from '../types/notification.types'

export async function listNotifications(
  params: ListNotificationsParams = {},
): Promise<NotificationsApiResponse> {
  const { data } = await api.get<NotificationsApiResponse>('/v1/notifications', {
    params,
  })

  if (!data.success) {
    throw new Error(data.message ?? 'Gagal memuatkan notifikasi.')
  }

  return data
}

export async function getNotificationCount(): Promise<NotificationCountApiResponse> {
  const { data } = await api.get<NotificationCountApiResponse>('/v1/notifications/count')

  if (!data.success) {
    throw new Error(data.message ?? 'Gagal memuatkan bilangan notifikasi.')
  }

  return data
}

export async function markNotificationAsRead(id: string): Promise<NotificationActionApiResponse> {
  const { data } = await api.patch<NotificationActionApiResponse>(`/v1/notifications/${id}/read`)

  if (!data.success) {
    throw new Error(data.message ?? 'Gagal menandakan notifikasi sebagai dibaca.')
  }

  return data
}

export async function markAllNotificationsAsRead(): Promise<NotificationActionApiResponse> {
  const { data } = await api.patch<NotificationActionApiResponse>('/v1/notifications/mark-all-read')

  if (!data.success) {
    throw new Error(data.message ?? 'Gagal menandakan semua notifikasi sebagai dibaca.')
  }

  return data
}
