import axios from 'axios';
import { getStoredLocale } from '@/lib/getLocale';

export const fetchNotifications = async (token: string, page: number = 1, limit: number = 10): Promise<any> => {
  try {
    const locale = getStoredLocale();
    const url = `${process.env.NEXT_PUBLIC_API_URL}/notifications?page=${page}&limit=${limit}`;
    const headers: Record<string, string> = {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`,
      'Accept-Language': locale,
      'Accept': 'application/json',
    };
    const response = await axios.get(url, { headers });
    const result = response?.data?.data;
    return {
      success: true,
      message: 'Fetch notifications success',
      data: result,
    };
  } catch (error: any) {
    throw error;
  }
};

export const markNotificationAsRead = async (id: string, token: string): Promise<any> => {
  try {
    const locale = getStoredLocale();
    const url = `${process.env.NEXT_PUBLIC_API_URL}/notification/${id}/mark-as-read`;
    const headers: Record<string, string> = {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`,
      'Accept-Language': locale,
      'Accept': 'application/json',
    };
    const response = await axios.get(url, { headers });
    const result = response?.data?.data;
    return {
      success: true,
      message: 'Notification marked as read',
      data: result,
    };
  } catch (error: any) {
    throw error;
  }
};

export const markNotificationAsUnread = async (id: string, token: string): Promise<any> => {
  try {
    const locale = getStoredLocale();
    const url = `${process.env.NEXT_PUBLIC_API_URL}/notification/${id}/mark-as-unread`;
    const headers: Record<string, string> = {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`,
      'Accept-Language': locale,
      'Accept': 'application/json',
    };
    const response = await axios.get(url, { headers });
    const result = response?.data?.data;
    return {
      success: true,
      message: 'Notification marked as unread',
      data: result,
    };
  } catch (error: any) {
    throw error;
  }
};


export const deleteNotification = async (id: string, token: string): Promise<any> => {
  try {
    const locale = getStoredLocale();
    const url = `${process.env.NEXT_PUBLIC_API_URL}/notification/${id}/delete`;
    const headers: Record<string, string> = {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`,
      'Accept-Language': locale,
      'Accept': 'application/json',
    };
    const response = await axios.delete(url, { headers });
    const result = response?.data?.data;
    return {
      success: true,
      message: 'Notification deleted.',
      data: result,
    };
  } catch (error: any) {
    throw error;
  }
};

export const markNotificationAsViewed = async (id: string, token: string): Promise<any> => {
  try {
    const locale = getStoredLocale();
    const url = `${process.env.NEXT_PUBLIC_API_URL}/notification/${id}/view`;
    const headers: Record<string, string> = {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`,
      'Accept-Language': locale,
      'Accept': 'application/json',
    };
    const response = await axios.get(url, { headers });
    const result = response?.data?.data;
    return {
      success: true,
      message: 'Notification marked as viewed',
      data: result,
    };
  } catch (error: any) {
    throw error;
  }
};

export const markAllNotificationsAsRead = async (token: string): Promise<any> => {
  try {
    const locale = getStoredLocale();
    const url = `${process.env.NEXT_PUBLIC_API_URL}/notifications/mark-as-read-all`;
    const headers: Record<string, string> = {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`,
      'Accept-Language': locale,
      'Accept': 'application/json',
    };

    const response = await axios.get(url, { headers });
    const result = response?.data?.data;

    return {
      success: true,
      message: response?.data?.message || 'All notifications marked as read successfully',
      data: result,
    };
  } catch (error: any) {
    throw error;
  }
};
