import { apiClient } from './client';

export interface Notification {
  id: string;
  userId: string;
  type: 'feeding' | 'sleep' | 'diaper' | 'medication' | 'milestone' | 'family' | 'system' | 'reminder';
  title: string;
  message: string;
  data?: Record<string, any>;
  isRead: boolean;
  isDismissed: boolean;
  createdAt: string;
  readAt?: string | null;
  dismissedAt?: string | null;
}

export interface GetNotificationsParams {
  limit?: number;
  offset?: number;
  isRead?: boolean;
  type?: Notification['type'];
}

export interface GetNotificationsResponse {
  notifications: Notification[];
  total: number;
  unreadCount: number;
}

export const notificationsApi = {
  /**
   * Get user notifications with optional filters
   */
  async getNotifications(params?: GetNotificationsParams): Promise<GetNotificationsResponse> {
    const { data } = await apiClient.get<GetNotificationsResponse>('/notifications', { params });
    return data;
  },

  /**
   * Mark a notification as read
   */
  async markAsRead(notificationId: string): Promise<Notification> {
    const { data } = await apiClient.patch<Notification>(`/notifications/${notificationId}/read`);
    return data;
  },

  /**
   * Mark all notifications as read
   */
  async markAllAsRead(): Promise<{ count: number }> {
    const { data } = await apiClient.patch<{ count: number }>('/notifications/mark-all-read');
    return data;
  },

  /**
   * Dismiss a notification
   */
  async dismiss(notificationId: string): Promise<Notification> {
    const { data } = await apiClient.patch<Notification>(`/notifications/${notificationId}/dismiss`);
    return data;
  },

  /**
   * Get unread notification count
   */
  async getUnreadCount(): Promise<number> {
    const { data } = await apiClient.get<GetNotificationsResponse>('/notifications', {
      params: { limit: 1, isRead: false },
    });
    return data.unreadCount;
  },
};
