import api from "../axios";
import { API_ENDPOINTS } from "../endpoints";
import {
  TCreateNotificationCredentials,
  TNotificationDetailsResponse,
  TNotificationHistoryResponse,
} from "../types/NotificationHistory";
import { ApiResponse } from "../types/shared";

export const notificationService = {
  getMyNotifications: async (
    params?: Record<string, any>,
  ): Promise<TNotificationHistoryResponse> => {
    try {
      return (
        await api.get(API_ENDPOINTS.NOTIFICATIONS.MY_NOTIFICATIONS, { params })
      ).data;
    } catch (error) {
      console.error(
        "ERROR Caught in notification service => getMyNotifications",
        error,
      );
      throw error;
    }
  },
  markNotificationAsSeen: async (id: string) => {
    try {
      return (await api.get(API_ENDPOINTS.NOTIFICATIONS.MARK_AS_SEEN(id))).data;
    } catch (error) {
      console.error(
        "ERROR Caught in notification service => markNotificationAsRead",
        error,
      );
      throw error;
    }
  },
  getNotifications: async (
    params?: Record<string, any>,
  ): Promise<TNotificationHistoryResponse> => {
    try {
      return (await api.get(API_ENDPOINTS.NOTIFICATIONS.HISTORY, { params }))
        .data;
    } catch (error) {
      console.error(
        "ERROR Caught in notification service => getNotifications",
        error,
      );
      throw error;
    }
  },
  getNotificationDetails: async (
    id: string,
  ): Promise<ApiResponse<TNotificationDetailsResponse>> => {
    try {
      return (await api.get(API_ENDPOINTS.NOTIFICATIONS.DETAILS(id))).data;
    } catch (error) {
      console.error(
        "ERROR Caught in notification service => getNotificationDetails",
        error,
      );
      throw error;
    }
  },
  createNotification: async ({
    credentials,
  }: {
    credentials: TCreateNotificationCredentials;
  }) => {
    try {
      return (await api.post(API_ENDPOINTS.NOTIFICATIONS.CREATE, credentials))
        .data;
    } catch (error) {
      console.error(
        "ERROR Caught in Notification service => Create Notification",
        error,
      );
      throw error;
    }
  },
};
