import * as api from "../service/BlogService";
import { useQuery, useMutation } from "@tanstack/react-query";
import { CreateBlogCategoryType, CreateBlogType } from "../types/BlogTypes";

export const useGetBlogCategories = () => {
  return useQuery({
    queryKey: ["blog-categories"],
    queryFn: api.getBlogCategories,
  });
};

export const useCreateBlogCategory = () => {
  return useMutation({
    mutationFn: api.createBlogCategory,
  });
};

export const useUpdateBlogCategory = () => {
  return useMutation({
    mutationFn: (data: { id: string; params: CreateBlogCategoryType }) =>
      api.updateBlogCategory(data.id, data.params),
  });
};

export const useDeleteBlogCategory = () => {
  return useMutation({
    mutationFn: api.deleteBlogCategory,
  });
};

export const useGetBlogCategoryDetail = (
  id: string,
  enabled: boolean = true
) => {
  return useQuery({
    queryKey: ["blog-category-detail", id],
    queryFn: () => api.getBlogCategoryDetail(id),
    enabled,
  });
};

export const useGetBlogs = (search: string) => {
  return useQuery({
    queryKey: ["blogs", search],
    queryFn: () => api.getBlogs(search),
  });
};

export const useGetBlogDetail = (id: string) => {
  return useQuery({
    queryKey: ["blog", id],
    queryFn: () => api.getBlogDetail(id),
  });
};

export const useCreateBlog = () => {
  return useMutation({
    mutationFn: api.createBlog,
  });
};

export const useUpdateBlog = () => {
  return useMutation({
    mutationFn: (data: { id: string; params: CreateBlogType }) =>
      api.updateBlog(data.id, data.params),
  });
};

export const useDeleteBlog = () => {
  return useMutation({
    mutationFn: api.deleteBlog,
  });
};

export const useGetBlogComments = () => {
  return useQuery({
    queryKey: ["blog-comments"],
    queryFn: api.getBlogComments,
  });
};

export const useChangeStatusComment = () => {
  return useMutation({
    mutationFn: (data: { id: string; status: string }) =>
      api.changeStatusComment(data.id, data.status),
  });
};
