import { getCollection } from "astro:content";
import rss from "@astrojs/rss";
import type { APIContext } from "astro";

export async function GET(context: APIContext) {
  const articles = await getCollection("blog", ({ data }) => !data.draft);

  const items = articles
    .sort((a, b) => b.data.publishDate.valueOf() - a.data.publishDate.valueOf())
    .map((post) => ({
      title: post.data.title,
      description: post.data.description,
      pubDate: post.data.publishDate,
      link: `/blog/${post.id}/`,
    }));

  return rss({
    title: "Adrian Altner — Blog",
    description: "Articles on technology, web development, and photography.",
    site: context.site ?? new URL("https://adrian-altner.com"),
    items,
    customData: "<language>en-us</language>",
  });
}
