import axios from "@/config/axios";
import {
  BlogCategoryResponse,
  BlogCombinedResponse,
  BlogResponse,
  BlogSingleResponse,
  CreateBlogCommentType,
} from "../types/BlogTypes";

export const getBlogs = async (categoryId: string) => {
  let query = "";
  if (categoryId) {
    query = `?categoryId=${categoryId}`;
  }
  const { data } = await axios.get<BlogResponse>(`/blogs${query}`);
  return data;
};

export const getMostVisitedBlogs = async () => {
  const { data } = await axios.get<BlogResponse>(`/blogs/most-visited`);
  return data;
};

export const getBlogCategories = async () => {
  const { data } = await axios.get<BlogCategoryResponse>(
    `/blogs/categories/public`
  );
  return data;
};

export const getBlogsPinned = async () => {
  const { data } = await axios.get<BlogResponse>(`/blogs/pinned`);
  return data;
};

export const getCombinedBlogs = async () => {
  const { data } = await axios.get<BlogCombinedResponse>(`/blogs/combined`);
  return data;
};

export const getBlogSingle = async (slug: string) => {
  const { data } = await axios.get<BlogSingleResponse>(`/blogs/slug/${slug}`);
  return data;
};

export const createBlogComment = async (
  id: string,
  params: CreateBlogCommentType
) => {
  const { data } = await axios.post<BlogSingleResponse>(
    `/blogs/${id}/comments`,
    params
  );
  return data;
};
