import { NextResponse } from 'next/server'
import { getDocMarkdown } from '@/app/lib/mdx-to-markdown'

// Map of valid slugs
const validSlugs = new Set([
    'introduction',
    'quick-start',
    'permissions',
    'verification',
    'lexicons/publication',
    'lexicons/document',
    'lexicons/subscription',
    'lexicons/theme',
    'implementations',
    'faq',
])

export async function generateStaticParams() {
    return Array.from(validSlugs).map((slug) => ({
        slug: slug.split('/'),
    }))
}

export async function GET(
    request: Request,
    { params }: { params: Promise<{ slug: string[] }> }
) {
    try {
        const { slug } = await params
        const slugPath = slug.join('/')

        if (!validSlugs.has(slugPath)) {
            return new NextResponse('Not found', { status: 404 })
        }

        const markdown = await getDocMarkdown(slugPath)

        return new NextResponse(markdown, {
            headers: {
                'Content-Type': 'text/plain; charset=utf-8',
            },
        })
    } catch (error) {
        console.error('API Markdown Error:', error)
        return new NextResponse('Internal server error', { status: 500 })
    }
}