import { useMutation, useQuery } from "@tanstack/react-query";
import * as api from "../service/BlogsService";
import type {
  GetBlogsResult,
  Blog,
  CreateBlogType,
  UpdateBlogType,
  BlogCategoriesResponse,
  CreateCategoryBlogType,
} from "../types/Types";
import { useSharedStore } from "@/shared/store/sharedStore";
export const useGetBlogs = (page: number = 1) => {
  const { search } = useSharedStore();
  return useQuery<GetBlogsResult>({
    queryKey: ["blogs", page, search],
    queryFn: () => api.getBlogs(page, search),
  });
};

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),
  });
};

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

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

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