import clientPromise, { database } from "@/db.config";
import { getOrigin } from "@/models/helper";

export async function generateSitemaps() {
  // Fetch the total number of posts and calculate the number of sitemaps needed
  return [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }];
}

export default async function sitemap({ id }) {
  // Google's limit is 50,000 URLs per sitemap
  const skip = (id - 1) * 50000;
  const limit = skip + 50000;

  const origin = await getOrigin();
  const client = await clientPromise;
  const db = client.db(database);
  const postCollection = db.collection("posts");

  const posts = await postCollection.find({}).skip(skip).limit(limit).toArray();

  return posts.map((post) => ({
    url: `${origin}/post/${post.slug}`,
    lastModified: post.modifiedAt,
  }));
}
