import rss from '@astrojs/rss';
import { getCollection } from 'astro:content';

export async function GET(context) {
  const [reviews, posts, finds] = await Promise.all([
    getCollection('reviews'),
    getCollection('posts'),
    getCollection('finds'),
  ]);

  const items = [
    ...reviews.map((r) => ({
      title: r.data.name,
      description: r.data.description ?? r.data.subtitle ?? '',
      pubDate: r.data.date,
      link: `/reviews/${r.id}/`,
      categories: [r.data.category, ...r.data.tags],
    })),
    ...posts.map((p) => ({
      title: p.data.title,
      description: p.data.description ?? '',
      pubDate: p.data.date,
      link: `/posts/${p.id}/`,
      categories: [p.data.category, ...p.data.tags],
    })),
    ...finds.map((f) => ({
      title: f.data.title,
      description: f.data.description ?? f.data.blurb ?? '',
      pubDate: f.data.date,
      link: `/finds/${f.id}/`,
      categories: [f.data.category, ...f.data.tags],
    })),
  ].sort((a, b) => b.pubDate.valueOf() - a.pubDate.valueOf());

  return rss({
    title: 'Unique',
    description:
      'A small log of delightful, unique things — products, apps, phenomena, oddities.',
    site: context.site,
    items,
    customData: '<language>en-us</language>',
  });
}
