import type { NotificationListResult } from "@edr/types";

import { client } from "@/utils/api";

export interface ListNotificationsParams {
  page?: number;
  limit?: number;
  isRead?: boolean;
}

/**
 * Portal notification REST calls. The portal axios `client` returns the raw
 * response, and the API wraps payloads in a `{ success, data }` envelope — so we
 * unwrap `.data.data` here (same convention as the other portal services).
 */
export const notificationsApi = {
  list: async (
    params: ListNotificationsParams = {},
  ): Promise<NotificationListResult> => {
    const { data } = await client.get("/api/notifications", { params });
    return data.data;
  },
  unreadCount: async (): Promise<number> => {
    const { data } = await client.get("/api/notifications/unread-count");
    return data.data.unreadCount;
  },
  markRead: async (id: string): Promise<void> => {
    await client.patch(`/api/notifications/${id}/read`);
  },
  markAllRead: async (): Promise<void> => {
    await client.post("/api/notifications/read-all");
  },
};
