import { useMutation, useQuery } from "@tanstack/react-query";
import * as api from "../service/BlogsService";
import type {
  GetBlogsResult,
  Blog,
  CreateBlogType,
  UpdateBlogType,
  BlogCategoriesResponse,
} from "../types/Types";

export const useGetBlogs = (page: number = 1) => {
  return useQuery<GetBlogsResult>({
    queryKey: ["blogs", page],
    queryFn: () => api.getBlogs(page),
  });
};

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

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

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

export const useUpdateBlog = () => {
  return useMutation({
    mutationFn: (params: UpdateBlogType) => api.updateBlog(params),
  });
};

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