import { getApiBaseUrl } from "@/components/main/BaseUrl";
import { Post } from "@/types/types";

export async function fetchBookmarkPosts(
  page: number,
  limit: number,
  token: string
): Promise<{ posts: Post[]; hasMore: boolean; totalItems: number }> {
  const base = getApiBaseUrl();
  const res = await fetch(
    `${base}/posts/bookmarks?page=${page}&limit=${limit}`,
    {
      cache: "no-store",
      headers: { Authorization: `Bearer ${token}` },
    }
  );

  if (!res.ok) {
    return { posts: [], hasMore: false, totalItems: 0 };
  }

  const data = await res.json();
  const posts = (data?.posts ?? []) as Post[];
  const totalItems = data?.totalItems ?? posts.length;
  const totalPages = data?.totalPages ?? 1;

  return {
    posts,
    hasMore: page < totalPages,
    totalItems,
  };
}
