import axios from "@/config/axios";
import { BlogResponse, BlogDetailResponse, BlogCategory } from "../types/Types";

export const getBlogs = async (
  page: number,
  limit: number
): Promise<BlogResponse> => {
  const { data } = await axios.get(`/blogs?page=${page}&limit=${limit}`);
  return data;
};

export const getBlogCategories = async (): Promise<BlogCategory[]> => {
  try {
    const { data } = await axios.get("/blogs/categories");
    return data.results?.categories || [];
  } catch (error) {
    console.error("Error fetching blog categories:", error);
    return [];
  }
};

export const getBlogDetails = async (
  id: string
): Promise<BlogDetailResponse> => {
  const { data } = await axios.get(`/blogs/${id}`);
  return data;
};

export const getBlogsByCategory = async (
  slug: string,
  page: number = 1,
  limit: number = 10
): Promise<BlogResponse> => {
  const { data } = await axios.get(
    `/blogs/category/${slug}?page=${page}&limit=${limit}`
  );
  return data;
};
