import * as api from "../service/Service";
import { useQuery } from "@tanstack/react-query";
import { BlogResponse, BlogDetailResponse, BlogCategory } from "../types/Types";

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

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

export const useGetBlogDetails = (id: string) => {
  return useQuery<BlogDetailResponse>({
    queryKey: ["blog-details", id],
    queryFn: () => api.getBlogDetails(id),
    enabled: !!id,
  });
};

export const useGetBlogsByCategory = (
  slug: string,
  page: number = 1,
  limit: number = 10
) => {
  return useQuery<BlogResponse>({
    queryKey: ["blogs-by-category", slug, page, limit],
    queryFn: () => api.getBlogsByCategory(slug, page, limit),
    enabled: !!slug,
  });
};

export const useGetLastBlogs = (limit: number = 3) => {
  return useQuery<BlogResponse>({
    queryKey: ["last-blogs", limit],
    queryFn: () => api.getBlogs(1, limit),
  });
};
