import { getCollection } from 'astro:content';
import type { APIRoute } from 'astro';

export const GET: APIRoute = async () => {
  try {
    const posts = await getCollection('posts');
    
    // Get the raw content from each post
    const postsWithContent = await Promise.all(
      posts.map(async (post) => {
        const { Content } = await post.render();
        
        // Get the raw markdown content by reading the file
        const rawContent = post.body;
        
        return {
          slug: post.slug,
          title: post.data.title,
          description: post.data.description,
          pubDate: post.data.pubDate.toISOString().split('T')[0],
          tags: post.data.tags || [],
          content: rawContent
        };
      })
    );
    
    return new Response(JSON.stringify(postsWithContent), {
      status: 200,
      headers: {
        'Content-Type': 'application/json'
      }
    });
  } catch (error) {
    console.error('Error fetching posts:', error);
    return new Response(JSON.stringify([]), {
      status: 500,
      headers: {
        'Content-Type': 'application/json'
      }
    });
  }
}; 