import apiClient from './api';

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

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

  /**
   * Create a new category
   * @param {Object} categoryData - Category data
   * @param {string} categoryData.name - Category name
   * @param {string} [categoryData.description] - Category description
   * @param {string} [categoryData.imagePath] - Category image path
   * @returns {Promise} Promise with the API response
   */
  createCategory: async (categoryData) => {
    try {
      const response = await apiClient.post('/admin/categories', categoryData);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Update a category
   * @param {string} id - Category ID
   * @param {Object} categoryData - Updated category data
   * @param {string} categoryData.name - Category name
   * @param {string} [categoryData.description] - Category description
   * @param {string} [categoryData.imagePath] - Category image path
   * @returns {Promise} Promise with the API response
   */
  updateCategory: async (id, categoryData) => {
    try {
      const response = await apiClient.put(`/admin/categories/${id}`, categoryData);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

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

export default categoryAdminService;