/**
 * Notification Service
 *
 * Handles API calls for in-app notifications.
 */

import { apiClient, getDataOrThrow } from "@/core/api";
import type { ActionAckResponse } from "@customer-portal/domain/common";
import type { NotificationListResponse } from "@customer-portal/domain/notifications";

const BASE_PATH = "/api/notifications";

export const notificationService = {
  /**
   * Get notifications for the current user
   */
  async getNotifications(params?: {
    limit?: number;
    offset?: number;
    includeRead?: boolean;
  }): Promise<NotificationListResponse> {
    const query: Record<string, string> = {};
    if (params?.limit) query["limit"] = String(params.limit);
    if (params?.offset) query["offset"] = String(params.offset);
    if (params?.includeRead !== undefined) query["includeRead"] = String(params.includeRead);

    const response = await apiClient.GET<NotificationListResponse>(BASE_PATH, {
      params: { query },
    });
    return getDataOrThrow(response);
  },

  /**
   * Get unread notification count
   */
  async getUnreadCount(): Promise<number> {
    const response = await apiClient.GET<{ count: number }>(`${BASE_PATH}/unread-count`);
    const data = getDataOrThrow(response);
    return data.count;
  },

  /**
   * Mark a notification as read
   */
  async markAsRead(notificationId: string): Promise<void> {
    await apiClient.POST<ActionAckResponse>(`${BASE_PATH}/${notificationId}/read`);
  },

  /**
   * Mark all notifications as read
   */
  async markAllAsRead(): Promise<void> {
    await apiClient.POST<ActionAckResponse>(`${BASE_PATH}/read-all`);
  },

  /**
   * Dismiss a notification
   */
  async dismiss(notificationId: string): Promise<void> {
    await apiClient.POST<ActionAckResponse>(`${BASE_PATH}/${notificationId}/dismiss`);
  },
};
