import apiClient from './api';

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

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

  /**
   * Create a new blog post (admin)
   * @param {Object} postData - Blog post data
   * @returns {Promise} Promise with the API response
   */
  createPost: async (postData) => {
    try {
      const response = await apiClient.post('/admin/blog', postData);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

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

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

  /**
   * Upload an image for a blog post (admin)
   * @param {string} postId - Blog post ID
   * @param {Object} imageData - Image data to upload
   * @returns {Promise} Promise with the API response
   */
  uploadImage: async (postId, imageData) => {
    try {
      const response = await apiClient.post(`/admin/blog/${postId}/images`, imageData);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Delete an image from a blog post (admin)
   * @param {string} postId - Blog post ID
   * @param {string} imageId - Image ID
   * @returns {Promise} Promise with the API response
   */
  deleteImage: async (postId, imageId) => {
    try {
      const response = await apiClient.delete(`/admin/blog/${postId}/images/${imageId}`);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

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

  /**
   * Get all comments for a post (admin)
   * @param {string} postId - Blog post ID
   * @returns {Promise} Promise with the API response
   */
  getPostComments: async (postId) => {
    try {
      const response = await apiClient.get(`/admin/blog-comments/posts/${postId}`);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

  /**
   * Approve a comment (admin)
   * @param {string} commentId - Comment ID
   * @returns {Promise} Promise with the API response
   */
  approveComment: async (commentId) => {
    try {
      const response = await apiClient.post(`/admin/blog-comments/${commentId}/approve`);
      return response.data;
    } catch (error) {
      throw error.response?.data || { message: 'An unknown error occurred' };
    }
  },

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

export default blogAdminService;