import { notFound } from "next/navigation";
import { readdirSync } from "fs";
import { join } from "path";

export async function generateStaticParams() {
  const blogDir = join(process.cwd(), "src/app/blog/posts");
  const files = readdirSync(blogDir).filter((file) => file.endsWith(".mdx"));

  return files.map((file) => ({
    slug: file.replace(".mdx", ""),
  }));
}

export default async function BlogPost({
  params,
}: {
  params: Promise<{ slug: string }>;
}) {
  const { slug } = await params;

  try {
    const Post = (
      await import(`@/app/blog/posts/${slug}.mdx`)
    ).default;

    return <Post />;
  } catch {
    notFound();
  }
}
