import ApiService from "./ApiService";

// Notification interfaces
export interface Notification {
  id: string;
  title: string;
  description: string;
  is_read: boolean;
  created_at: string;
  updated_at: string;
  type: string;
}

export interface NotificationFilters {
  search?: string;
  is_read?: boolean;
  page?: number;
  limit?: number;
}

export interface SaveTokenData {
  fcm_token: string;
}

export interface MarkReadData {
  notification_id?: string;
  mark_all_read?: boolean;
}

export interface NotificationListResponse {
  success: boolean;
  message?: string;
  data: {
    notifications: {
      data: Notification[];
      total: number;
      page: number;
      limit: number;
    };
    unread_count: number;
    total: number;
    page: number;
    limit: number;
  };
}

export interface SaveTokenResponse {
  success: boolean;
  message?: string;
  data?: any;
}

export interface MarkReadResponse {
  success: boolean;
  message?: string;
  data?: any;
}

// Save FCM Token
export async function saveToken(data: SaveTokenData) {
  return ApiService.request<SaveTokenResponse>({
    url: "/notifications/save-token",
    method: "post",
    data,
  });
}

// Get Notifications with filters
export async function getNotifications(filters?: NotificationFilters) {
  const params: any = {
    page: filters?.page || 1,
    limit: filters?.limit || 20,
  };

  if (filters?.search) {
    params.search = filters.search;
  }

  if (filters?.is_read !== undefined) {
    params.is_read = filters.is_read;
  }

  return ApiService.request<NotificationListResponse>({
    url: "/notifications/list",
    method: "get",
    params,
  });
}

// Mark Notification as Read
export async function markAsRead(data: MarkReadData) {
  return ApiService.request<MarkReadResponse>({
    url: "/notifications/mark-read",
    method: "post",
    data,
  });
}

