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

export 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.search) searchParams.set('search', filters.search);
  if (filters.status) searchParams.set('status', filters.status);
  if (filters.type) searchParams.set('type', filters.type);

  return searchParams;
}

export async function getNotifications(filters: NotificationFilters): Promise<NotificationsResponse> {
  const searchParams = buildNotificationSearchParams(filters);
  return apiClient<NotificationsResponse>(`/notifications?${searchParams.toString()}`);
}

export async function markNotificationAsRead(id: number) {
  return apiClient<NotificationMutationResponse>(`/notifications/${id}/read`, {
    method: 'PATCH'
  });
}

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