import apiClient from './api';

export const productService = {
  /**
   * Get all products with optional filtering
   * @param {Object} params - Query parameters
   * @param {string} [params.category] - Filter by category name
   * @param {string} [params.tag] - Filter by tag name
   * @param {string} [params.search] - Search term
   * @param {string} [params.sort] - Sort field
   * @param {string} [params.order] - Sort order ('asc' or 'desc')
   * @returns {Promise} Promise with the API response
   */
  getAllProducts: async (params = {}) => {
    try {
      const response = await apiClient.get('/products', { params });
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

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

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

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

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

// Admin-only product service functions
export const productAdminService = {
  /**
   * Create a new product (admin only)
   * @param {Object} productData - Product data
   * @returns {Promise} Promise with the API response
   */
  createProduct: async (productData) => {
    try {
      const response = await apiClient.post('/admin/products', productData);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Update a product (admin only)
   * @param {string} id - Product ID
   * @param {Object} productData - Updated product data
   * @returns {Promise} Promise with the API response
   */
  updateProduct: async (id, productData) => {
    try {
      const response = await apiClient.put(`/admin/products/${id}`, productData);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

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

export default productService;