import { useMutation, useQuery } from "@tanstack/react-query";
import type { BlogPost } from "@/api/blog-api";
import { getBlogPost, updateBlogPost } from "@/api/blog-api";

export function useBlogPost(id: string) {
	const {
		data: post,
		isLoading,
		error,
	} = useQuery({
		queryKey: ["blog-post", id],
		queryFn: () => getBlogPost(id),
		enabled: !!id,
	});

	const mutation = useMutation({
		mutationFn: (data: BlogPost) => updateBlogPost(id, data),
	});

	return {
		post,
		isLoading,
		error,
		update: mutation.mutateAsync,
		updating: mutation.isPending,
		updateError: mutation.error,
	};
}
