import { getCollection } from "astro:content";
import { buildArticleVNode, renderOgImage } from "@/lib/og";

export async function getStaticPaths() {
  const posts = await getCollection("blog", ({ data }) => !data.draft);
  return posts.map((post) => ({
    params: { slug: post.id },
    props: { title: post.data.title, description: post.data.description },
  }));
}

export async function GET({
  props,
}: {
  props: { title: string; description: string };
}) {
  const buffer = await renderOgImage(
    buildArticleVNode({ title: props.title, description: props.description }),
  );
  return new Response(buffer, {
    headers: { "Content-Type": "image/png" },
  });
}
