import rss, { type RSSOptions } from '@astrojs/rss';
import type { APIContext } from 'astro';
import { GLOBAL } from '../lib/variables';
import { articles } from '../lib/list';

export async function GET(context: APIContext) {
  return rss({
    // The RSS feed title, description and custom metadata
    title: `${GLOBAL.articlesName} • ${GLOBAL.username}`,
    description: GLOBAL.blogLongDescription,
    site: context.site || GLOBAL.rootUrl,
    
    // Add custom RSS feed metadata
    customData: `
      <language>en</language>
      <lastBuildDate>${new Date().toUTCString()}</lastBuildDate>
      <generator>Astro with @astrojs/rss</generator>
    `,
    
    // Items for the RSS feed - our blog articles
    items: articles.map((article) => ({
      title: article.title,
      description: article.description,
      link: `${GLOBAL.rootUrl}${article.filename}`,
      pubDate: new Date(article.timestamp),
      categories: article.tags,
      // Optional - content full article content as CDATA
      // content: article.content,
    })),
    
    // Optional stylesheet for the XML
    stylesheet: '/rss/styles.xsl',
  });
}
