import type { Metadata } from "next";
import { BlogPostPage } from "@blog/next-rsc";
import { FeaturedSidebar, Subscribe, ScrollLogger } from "@blog";
import { generateBlogPostStaticParams } from "@blog/server";
import { generateBlogPostMetadata } from "@blog/next";
import { blogConfig } from "@/lib/blog-config";
import Header from "@/components/header";
import Footer from "@/components/footer";
import { metadata as rootMetadata } from "@/app/layout";

export const revalidate = 604800;
export const generateStaticParams = () =>
  generateBlogPostStaticParams(blogConfig);

export async function generateMetadata({
  params,
}: {
  params: Promise<{ slug: string }>;
}): Promise<Metadata> {
  const { slug } = await params;
  return generateBlogPostMetadata(blogConfig, slug, rootMetadata);
}

export default async function Page({
  params,
}: {
  params: Promise<{ slug: string }>;
}) {
  const { slug } = await params;
  const post = await blogConfig.feed.fetchDocument(slug);

  return (
    <BlogPostPage
      config={blogConfig}
      slug={slug}
      header={<Header />}
      footer={<Footer />}
      beforeArticle={
        <ScrollLogger eventName="blog.post.scroll" data={{ post_slug: slug }} />
      }
      sidebar={
        post && (
          <FeaturedSidebar
            config={blogConfig}
            postCategories={post.categories}
            excludeHrefs={[post.href]}
            afterHeading={
              <Subscribe publicationUri={blogConfig.sequoia.publicationUri} />
            }
          />
        )
      }
    />
  );
}
