import { apiClient } from '@/lib/api/client';
import type { InboxPage, UserNotificationItem } from '@/types/notifications';
import type { LabCaseActivityItem } from '@/types/lab-case-activity';
import type { LabCaseTabReadTarget, TabBadgeCounts } from '@/lib/tabBadgeUtils';

export const notificationsApi = {
  tabCounts: async (): Promise<{ success: boolean; data: TabBadgeCounts }> => {
    const response = await apiClient.get('/notifications/tab-counts');
    return response.data;
  },

  listLabCaseActivities: async (
    labCaseId: string,
    limit = 50,
  ): Promise<{ success: boolean; data: LabCaseActivityItem[] }> => {
    const response = await apiClient.get(`/notifications/lab-cases/${labCaseId}/activities`, {
      params: { limit },
    });
    return response.data;
  },

  markTabRead: async (tab: LabCaseTabReadTarget): Promise<{ success: boolean }> => {
    const response = await apiClient.post('/notifications/mark-tab-read', { tab });
    return response.data;
  },

  markCaseRead: async (labCaseId: string): Promise<{ success: boolean }> => {
    const response = await apiClient.post('/notifications/mark-case-read', { labCaseId });
    return response.data;
  },

  listInbox: async (params?: {
    limit?: number;
    cursor?: string;
  }): Promise<{ success: boolean; data: InboxPage }> => {
    const response = await apiClient.get('/notifications/inbox', { params });
    return response.data;
  },

  inboxUnreadCount: async (): Promise<{ success: boolean; data: { count: number } }> => {
    const response = await apiClient.get('/notifications/inbox/unread-count');
    return response.data;
  },

  markInboxRead: async (
    id: string,
  ): Promise<{ success: boolean; data: UserNotificationItem | null }> => {
    const response = await apiClient.post(`/notifications/inbox/${id}/read`);
    return response.data;
  },

  markInboxReadAll: async (): Promise<{ success: boolean }> => {
    const response = await apiClient.post('/notifications/inbox/read-all');
    return response.data;
  },
};
