import apiGlobal from '@/services/apiGlobal';

export interface AdminNotification {
  _id: string;
  title: string;
  message: string;
  type: 'signup' | 'volunteer_form' | 'order' | 'admin_alert' | 'NEW_SIGNUP' | 'VOLUNTEER_FORM' | 'NEW_ORDER' | 'ORDER_STATUS' | 'URGENT_ALERT';
  isRead: boolean;
  createdAt: string;
  updatedAt: string;
  priority: 'low' | 'medium' | 'high' | 'urgent' | 'LOW' | 'MEDIUM' | 'HIGH' | 'URGENT';
  relatedId?: string;
  relatedModel?: string;
  actionUrl?: string;
  relatedData?: {
    userId?: string;
    orderId?: string;
    formId?: string;
    url?: string;
  };
  relatedUserData?: {
    fullName?: string;
    emailAddress?: string;
  };
}

export interface NotificationFilters {
  skip?: number;
  limit?: number;
  type?: string;
  isRead?: boolean;
  priority?: string;
  sortBy?: string;
  sortOrder?: 'asc' | 'desc';
}

export interface NotificationStats {
  totalNotifications: number;
  unreadCount: number;
  todayCount: number;
  typeBreakdown: {
    [key: string]: number;
  };
}

export interface NotificationsResponse {
  success: boolean;
  message: string;
  data: {
    notifications: AdminNotification[];
    pagination: {
      currentPage: number;
      totalPages: number;
      totalCount: number;
      limit: number;
      skip: number;
      total: number;
      unread: number;
      page: number;
      pages: number;
    };
    stats?: NotificationStats;
  };
}

// Fetch admin notifications
export const fetchAdminNotifications = async (
  filters: NotificationFilters = {}
): Promise<NotificationsResponse> => {
  const params = new URLSearchParams();
  
  if (filters.skip) params.append('skip', filters.skip.toString());
  if (filters.limit) params.append('limit', filters.limit.toString());
  if (filters.type) params.append('type', filters.type);
  if (filters.isRead !== undefined) params.append('isRead', filters.isRead.toString());
  if (filters.priority) params.append('priority', filters.priority);
  if (filters.sortBy) params.append('sortBy', filters.sortBy);
  if (filters.sortOrder) params.append('sortOrder', filters.sortOrder);

  const url = `/notifications${params.toString() ? '?' + params.toString() : ''}`;
  const response = await apiGlobal.get(url);
  return response.data;
};

// Mark notifications as read
export const markNotificationsAsRead = async (
  notificationIds: string[]
): Promise<{ success: boolean; message: string }> => {
  const response = await apiGlobal.put('/notifications/mark-read', {
    notificationIds
  });
  return response.data;
};

// Mark all notifications as read
export const markAllNotificationsAsRead = async (): Promise<{ success: boolean; message: string }> => {
  const response = await apiGlobal.put('/notifications/mark-all-read');
  return response.data;
};

// Delete notification
export const deleteNotification = async (
  notificationId: string
): Promise<{ success: boolean; message: string }> => {
  const response = await apiGlobal.delete(`/notifications/${notificationId}`);
  return response.data;
};

// Get notification stats
export const getNotificationStats = async (): Promise<{
  success: boolean;
  data: NotificationStats;
}> => {
  const response = await apiGlobal.get('/notifications/stats');
  return response.data;
};

// Register FCM token
export const registerFCMToken = async (
  fcmToken: string,
  platform: string = 'web'
): Promise<{ success: boolean; message: string }> => {
  const response = await apiGlobal.post('/fcm/register', {
    fcmToken,
    platform
  });
  return response.data;
};

// Test notification (for development)
export const sendTestNotification = async (
  title: string,
  message: string
): Promise<{ success: boolean; message: string }> => {
  const response = await apiGlobal.post('/notifications/test', {
    title,
    message
  });
  return response.data;
};
