import apiClient from './authApi';

export interface Notification {
  notificationId: string;
  userId: string;
  requestId?: string;
  notificationType: string;
  title: string;
  message: string;
  isRead: boolean;
  priority: 'LOW' | 'MEDIUM' | 'HIGH' | 'URGENT';
  actionUrl?: string;
  actionRequired: boolean;
  metadata?: any;
  readAt?: string;
  createdAt: string;
}

export interface PushSubscription {
  subscriptionId: string;
  endpoint: string;
  userAgent?: string;
  createdAt: string;
}

const notificationApi = {
  /**
   * Get user's notifications with pagination
   */
  async list(params?: { page?: number; limit?: number; unreadOnly?: boolean }) {
    const response = await apiClient.get('/notifications', { params });
    return response.data;
  },

  /**
   * Get unread notification count
   */
  async getUnreadCount() {
    const response = await apiClient.get('/notifications/unread-count');
    return response.data;
  },

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

  /**
   * Mark all notifications as read
   */
  async markAllAsRead() {
    const response = await apiClient.post('/notifications/mark-all-read');
    return response.data;
  },

  /**
   * Delete notification
   */
  async delete(notificationId: string) {
    const response = await apiClient.delete(`/notifications/${notificationId}`);
    return response.data;
  }
};

/**
 * Get current user's push notification subscriptions
 */
export const getUserSubscriptions = async (): Promise<PushSubscription[]> => {
  const response = await apiClient.get<{ success: boolean; data: { subscriptions: PushSubscription[]; count: number } }>(
    '/notifications/subscriptions'
  );
  return response.data.data.subscriptions;
};

export default notificationApi;
