import apiClient from './api';

export const settingsAdminService = {
  /**
   * Get all settings grouped by category
   * @returns {Promise} Promise with the API response
   */
  getAllSettings: async () => {
    try {
      const response = await apiClient.get('/admin/settings');
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Get settings by category
   * @param {string} category - Category name
   * @returns {Promise} Promise with the API response
   */
  getSettingsByCategory: async (category) => {
    try {
      const response = await apiClient.get(`/admin/settings/category/${category}`);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Get a specific setting
   * @param {string} key - Setting key
   * @returns {Promise} Promise with the API response
   */
  getSetting: async (key) => {
    try {
      const response = await apiClient.get(`/admin/settings/${key}`);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Update a single setting
   * @param {string} key - Setting key
   * @param {string} value - Setting value
   * @param {string} category - Setting category
   * @returns {Promise} Promise with the API response
   */
  updateSetting: async (key, value, category) => {
    try {
      const response = await apiClient.put(`/admin/settings/${key}`, { value, category });
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Update multiple settings at once
   * @param {Array} settings - Array of setting objects {key, value, category}
   * @returns {Promise} Promise with the API response
   */
  updateSettings: async (settings) => {
    try {
      const response = await apiClient.post('/admin/settings/batch', { settings });
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Delete a setting
   * @param {string} key - Setting key
   * @returns {Promise} Promise with the API response
   */
  deleteSetting: async (key) => {
    try {
      const response = await apiClient.delete(`/admin/settings/${key}`);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  }
};

export default settingsAdminService;