import { apiClient } from '@/shared/api/client'
import type { ApiResponse, PaginatedResult } from '@/shared/types/types'

export type AdminNotificationType = 'system' | 'chat' | (string & {})

export interface AdminNotification {
  id: number
  admin_user_id: number
  type: AdminNotificationType
  title: string
  content: string
  is_read: boolean
  created_at: string
  updated_at: string
}

export interface AdminNotificationQuery {
  read?: 'unread'
  page?: number
  page_size?: number
}

export async function fetchAdminNotifications(query: AdminNotificationQuery = {}) {
  const params = Object.fromEntries(
    Object.entries(query).filter(([, value]) => value !== '' && value !== undefined)
  )
  const { data } = await apiClient.get<ApiResponse<PaginatedResult<AdminNotification>>>(
    '/admin/notifications',
    { params }
  )
  return data.data
}

export async function fetchAdminNotificationUnreadCount() {
  const { data } = await apiClient.get<ApiResponse<{ unread_count: number }>>(
    '/admin/notifications/unread-count',
    {
      silent: true,
    }
  )
  return data.data.unread_count
}

export async function markAdminNotificationRead(id: number) {
  const { data } = await apiClient.post<ApiResponse<{ read: boolean }>>(
    `/admin/notifications/${id}/read`
  )
  return data.data
}

export async function markAllAdminNotificationsRead() {
  const { data } = await apiClient.put<ApiResponse<{ read_count: number }>>(
    '/admin/notifications/read-all'
  )
  return data.data
}
