import apiClient from './client';

export interface Notification {
  id: number;
  verb: string;
  read: boolean;
  timestamp: string;
  data: Record<string, any>;
  actor_type: string | null;
  actor_display: string | null;
  target_type: string | null;
  target_display: string | null;
  target_url: string | null;
}

export interface UnreadCountResponse {
  count: number;
}

/**
 * Get all notifications for the current user
 */
export const getNotifications = async (params?: { read?: boolean; limit?: number }): Promise<Notification[]> => {
  const queryParams = new URLSearchParams();
  if (params?.read !== undefined) {
    queryParams.append('read', String(params.read));
  }
  if (params?.limit !== undefined) {
    queryParams.append('limit', String(params.limit));
  }
  const query = queryParams.toString();
  const url = query ? `/notifications/?${query}` : '/notifications/';
  const response = await apiClient.get(url);
  return response.data;
};

/**
 * Get count of unread notifications
 */
export const getUnreadCount = async (): Promise<number> => {
  const response = await apiClient.get<UnreadCountResponse>('/notifications/unread_count/');
  return response.data.count;
};

/**
 * Mark a single notification as read
 */
export const markNotificationRead = async (id: number): Promise<void> => {
  await apiClient.post(`/notifications/${id}/mark_read/`);
};

/**
 * Mark all notifications as read
 */
export const markAllNotificationsRead = async (): Promise<void> => {
  await apiClient.post('/notifications/mark_all_read/');
};

/**
 * Delete all read notifications
 */
export const clearAllNotifications = async (): Promise<void> => {
  await apiClient.delete('/notifications/clear_all/');
};
