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

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

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

  return rss({
    title: "Adrian Altner — Notes",
    description: "Short-form notes and observations.",
    site: context.site ?? new URL("https://adrian-altner.com"),
    items,
    customData: "<language>en-us</language>",
  });
}
