import blogs from "@/data/blogs.json";
import { getArticleSchema } from "@/lib/schemas";
import { notFound } from "next/navigation";
import BlogDetailClient from "./BlogDetailClient";

export function generateStaticParams() {
  return blogs.map((blog) => ({
    slug: blog.slug,
  }));
}

export function generateMetadata({ params }) {
  const blog = blogs.find((b) => b.slug === params.slug);
  if (!blog) return {};

  return {
    title: blog.title,
    description: blog.excerpt,
    keywords: [
      "Rajbari railway",
      "train news",
      blog.title,
      "Bangladesh Railway",
    ],
    openGraph: {
      title: `${blog.title} | Trains of Rajbari`,
      description: blog.excerpt,
      url: `https://trainsofrajbari.com/blog/${blog.slug}`,
      type: "article",
      publishedTime: blog.date,
      authors: [blog.author],
      images: [{ url: blog.image || "/logo.png" }],
    },
    alternates: {
      canonical: `https://trainsofrajbari.com/blog/${blog.slug}`,
    },
  };
}

export default function BlogDetailPage({ params }) {
  const blog = blogs.find((b) => b.slug === params.slug);
  if (!blog) notFound();

  return (
    <>
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{
          __html: JSON.stringify(
            getArticleSchema({
              title: blog.title,
              description: blog.excerpt,
              author: blog.author,
              datePublished: blog.date,
              image: blog.image
                ? `https://trainsofrajbari.com${blog.image}`
                : "https://trainsofrajbari.com/logo.png",
              url: `https://trainsofrajbari.com/blog/${blog.slug}`,
            })
          ),
        }}
      />
      <BlogDetailClient blog={blog} />
    </>
  );
}
