import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { LifeArticlePage } from "@/components/life-article-page";
import { lifePosts, getLifePost } from "@/content/life/posts";

type PageProps = {
  params: Promise<{
    slug: string;
  }>;
};

export function generateStaticParams() {
  return lifePosts.map((post) => ({ slug: post.slug }));
}

export async function generateMetadata({ params }: PageProps): Promise<Metadata> {
  const { slug } = await params;
  const post = getLifePost(slug);

  if (!post) {
    return {
      title: "文章未找到 | fly",
    };
  }

  return {
    title: `${post.title} | fly 日常分享`,
    description: post.description,
  };
}

export default async function LifeArticlePageRoute({ params }: PageProps) {
  const { slug } = await params;
  const post = getLifePost(slug);

  if (!post) {
    notFound();
  }

  const currentIndex = lifePosts.findIndex((p) => p.slug === slug);
  const prevPost = currentIndex > 0 ? lifePosts[currentIndex - 1] : undefined;
  const nextPost = currentIndex < lifePosts.length - 1 ? lifePosts[currentIndex + 1] : undefined;

  return <LifeArticlePage post={post} prevPost={prevPost} nextPost={nextPost} />;
}
