import { getLLMText, getSource } from '@/lib/source';
import { getCurrentSession } from '@/lib/session';
import { notFound } from 'next/navigation';

export async function GET(_req: Request, { params }: RouteContext<'/llms.mdx/docs/[[...slug]]'>) {
  const session = await getCurrentSession();
  if (!session) return new Response('Unauthorized', { status: 401 });

  const { slug } = await params;
  const page = getSource('member').getPage(slug?.slice(0, -1));
  if (!page) notFound();

  return new Response(await getLLMText(page), {
    headers: {
      'Content-Type': 'text/markdown',
    },
  });
}
