import { NextResponse } from "next/server";
import { getPublishedBlogBySlug } from "../../../../lib/blogs";

export const dynamic = "force-dynamic";

export async function GET(_request, { params }) {
  try {
    const { slug } = await params;
    const blog = await getPublishedBlogBySlug(slug);

    if (!blog) {
      return NextResponse.json({ error: "Blog not found." }, { status: 404 });
    }

    return NextResponse.json({ blog });
  } catch (error) {
    return NextResponse.json(
      { error: "Unable to load the blog." },
      { status: 500 }
    );
  }
}
