import {NextResponse} from 'next/server';
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';

export async function GET() {
    const projectsDirectory = path.join(process.cwd(), 'public/projects');
    const filenames = fs.readdirSync(projectsDirectory);

    console.log('Filenames:', filenames);

    const projects = filenames.map((filename) => {
        const filePath = path.join(projectsDirectory, filename);
        const fileContents = fs.readFileSync(filePath, 'utf8');
        const {data} = matter(fileContents);

        return {
            id: data.id,
            title: data.title,
            description: data.description,
            slug: filename.replace('.md', ''),
        };
    });

    return NextResponse.json(projects);
}