import { apiClient } from '@/lib/api-client';
import type {
  NotificationFilters,
  NotificationListResponse,
  NotificationMutationResponse,
  NotificationUnreadCountResponse
} from './types';

function buildNotificationSearchParams(filters: NotificationFilters) {
  const searchParams = new URLSearchParams();
  if (filters.page) {
    searchParams.set('page', String(filters.page));
  }
  if (filters.limit) {
    searchParams.set('limit', String(filters.limit));
  }
  if (filters.status) {
    searchParams.set('status', filters.status);
  }

  return searchParams.toString();
}

export async function getNotifications(filters: NotificationFilters = {}) {
  const search = buildNotificationSearchParams(filters);
  return apiClient<NotificationListResponse>(`/notifications${search ? `?${search}` : ''}`);
}

export async function getUnreadNotificationCount() {
  return apiClient<NotificationUnreadCountResponse>('/notifications/unread-count');
}

export async function markNotificationRead(id: string) {
  return apiClient<NotificationMutationResponse>(`/notifications/${id}/read`, {
    method: 'POST'
  });
}

export async function markAllNotificationsRead() {
  return apiClient<NotificationMutationResponse>('/notifications/read-all', {
    method: 'POST'
  });
}

export async function archiveNotificationItem(id: string) {
  return apiClient<NotificationMutationResponse>(`/notifications/${id}/archive`, {
    method: 'POST'
  });
}
