import apiClient from './client'
import type {
  NotificationConfig,
  NotificationConfigCreate,
  NotificationConfigUpdate,
  AppNotification,
  UnreadCount,
} from '@/types/notification'

export const notificationsApi = {
  async listConfigs(): Promise<NotificationConfig[]> {
    const response = await apiClient.get<NotificationConfig[]>('/notifications/configs')
    return response.data
  },

  async createConfig(data: NotificationConfigCreate): Promise<NotificationConfig> {
    const response = await apiClient.post<NotificationConfig>('/notifications/configs', data)
    return response.data
  },

  async updateConfig(id: string, data: NotificationConfigUpdate): Promise<NotificationConfig> {
    const response = await apiClient.patch<NotificationConfig>(`/notifications/configs/${id}`, data)
    return response.data
  },

  async deleteConfig(id: string): Promise<void> {
    await apiClient.delete(`/notifications/configs/${id}`)
  },

  async testConfig(configId: string): Promise<{ success: boolean; message: string }> {
    const response = await apiClient.post<{ success: boolean; message: string }>(
      '/notifications/configs/test',
      { config_id: configId }
    )
    return response.data
  },

  async list(params?: { skip?: number; limit?: number }): Promise<AppNotification[]> {
    const response = await apiClient.get<AppNotification[]>('/notifications', { params })
    return response.data
  },

  async unreadCount(): Promise<number> {
    const response = await apiClient.get<UnreadCount>('/notifications/unread-count')
    return response.data.count
  },

  async markRead(id: string): Promise<void> {
    await apiClient.patch(`/notifications/${id}/read`)
  },

  async markAllRead(): Promise<void> {
    await apiClient.post('/notifications/mark-all-read')
  },
}

export default notificationsApi
