// src/pages/search-index.json.js
// Generates a JSON file with all posts for client-side search

import { getCollection } from 'astro:content';

export async function get() {
  // Get all posts
  const allPosts = await getCollection('posts', ({ data }) => {
    // Exclude draft posts in production
    return import.meta.env.PROD ? !data.draft : true;
  });
  
  // Transform posts into search-friendly format
  const searchablePosts = allPosts.map(post => ({
    slug: post.slug,
    title: post.data.title,
    description: post.data.description || '',
    pubDate: post.data.pubDate ? new Date(post.data.pubDate).toISOString() : '',
    category: post.data.category || 'Uncategorized',
    tags: post.data.tags || [],
    readTime: post.data.readTime || '5 min read',
  }));
  
  // Return JSON
  return {
    body: JSON.stringify(searchablePosts),
    headers: {
      'Content-Type': 'application/json',
      'Cache-Control': 'max-age=3600'
    }
  }
}