import axios from "../../../config/axios";
import type {
  BlogsResponse,
  GetBlogsResult,
  Blog,
  CreateBlogType,
  UpdateBlogType,
  BlogCategoriesResponse,
  BlogDetailResponse,
} from "../types/Types";

export const getBlogs = async (page: number = 1): Promise<GetBlogsResult> => {
  const { data } = await axios.get<BlogsResponse>(`/blogs?page=${page}`);
  return data.results;
};

export const getBlogCategories = async (): Promise<BlogCategoriesResponse> => {
  const { data } = await axios.get<BlogCategoriesResponse>("/blogs/categories");
  return data;
};

export const getBlogDetail = async (id: string): Promise<Blog> => {
  const { data } = await axios.get<BlogDetailResponse>(`/blogs/${id}`);
  return data.results.blog;
};

export const createBlog = async (params: CreateBlogType) => {
  const { data } = await axios.post(`/admin/blog`, params);
  return data;
};

export const updateBlog = async (params: UpdateBlogType) => {
  const { _id, ...updateData } = params;
  const { data } = await axios.post(`/admin/blog/${_id}`, updateData);
  return data;
};

export const deleteBlog = async (id: string) => {
  const { data } = await axios.delete(`/admin/blog/${id}`);
  return data;
};
