import apiClient from '@/utils/axios.js';

export const listBlogs = async () => (await apiClient.get('/api/blog/blogs')).data;
export const getBlog = async (id) => (await apiClient.get(`/api/blog/blogs/${id}`)).data;
export const listPosts = async (id, { page = 1, pageSize = 10 } = {}) => (await apiClient.get(`/api/blog/blogs/${id}/posts`, { params: { page, pageSize } })).data;

export const createBlog = async (payload) => (await apiClient.post('/api/blog/blogs', payload)).data;
export const updateBlog = async (id, payload) => (await apiClient.put(`/api/blog/blogs/${id}`, payload)).data;
export const createPost = async (id, payload) => (await apiClient.post(`/api/blog/blogs/${id}/posts`, payload)).data;
export const listBlogImages = async (id) => (await apiClient.get(`/api/blog/blogs/${id}/images`)).data;
export const uploadBlogImage = async (id, file, meta = {}) => {
	const formData = new FormData();
	formData.append('image', file);
	if (meta.title) formData.append('title', meta.title);
	if (meta.description) formData.append('description', meta.description);
	return (await apiClient.post(`/api/blog/blogs/${id}/images`, formData, { headers: { 'Content-Type': 'multipart/form-data' } })).data;
};
export const shareBlog = async (id, payload) => (await apiClient.post(`/api/blog/blogs/${id}/share`, payload)).data;
 