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

// برای client
export const useGetBlogs = (categoryId: string) =>
  useQuery({
    queryKey: ["blogs", categoryId],
    queryFn: () => api.getBlogs(categoryId),
  });

export const useGetMostVisitedBlogs = () =>
  useQuery({
    queryKey: ["most-visited-blogs"],
    queryFn: api.getMostVisitedBlogs,
  });

export const useGetBlogsPinned = () =>
  useQuery({
    queryKey: ["blogs-pinned"],
    queryFn: api.getBlogsPinned,
  });

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

export const useGetBlogCombined = () =>
  useQuery({
    queryKey: ["blog-combined"],
    queryFn: api.getCombinedBlogs,
  });

export const useGetBlogSingle = (id: string) =>
  useQuery({
    queryKey: ["blog-single", id],
    queryFn: () => api.getBlogSingle(id),
  });

export const useCreateBlogComment = () =>
  useMutation({
    mutationFn: (variables: { id: string; params: CreateBlogCommentType }) =>
      api.createBlogComment(variables.id, variables.params),
  });

// برای server prefetch
export const blogsQueryOptions = {
  queryKey: ["blogs", ""],
  queryFn: () => api.getBlogs(""),
};

export const blogSingleQueryOptions = (id: string) => ({
  queryKey: ["blog-single", id],
  queryFn: () => api.getBlogSingle(id),
});

export const mostVisitedBlogsQueryOptions = {
  queryKey: ["most-visited-blogs"],
  queryFn: api.getMostVisitedBlogs,
};

export const blogsPinnedQueryOptions = {
  queryKey: ["blogs-pinned"],
  queryFn: api.getBlogsPinned,
};

export const blogCategoriesQueryOptions = {
  queryKey: ["blog-categories"],
  queryFn: api.getBlogCategories,
};

export const blogCombinedQueryOptions = {
  queryKey: ["blog-combined"],
  queryFn: api.getCombinedBlogs,
};
