import apiClient from './api';

const blogService = {
  /**
   * Get all published blog posts with optional filtering
   * @param {Object} params - Query parameters for filtering posts
   * @returns {Promise} Promise with the API response
   */
  getAllPosts: async (params = {}) => {
    try {
      const response = await apiClient.get('/blog', { params });
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

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

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

  /**
   * Add a comment to a blog post
   * @param {string} postId - Blog post ID
   * @param {Object} commentData - Comment data to submit
   * @param {string} commentData.userId - User ID
   * @returns {Promise} Promise with the API response
   */
  addComment: async (postId, commentData) => {
    try {
      const response = await apiClient.post(`/blog/${postId}/comments`, commentData);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  }
};

export default blogService;