import { getDictionary } from "@/get-dictionary";
import type { Locale } from "@/i18n-config";
import BlogClient from "@/components/BlogClient";
import { getBlogPosts } from "@/app/actions";

export const revalidate = 60;

export async function generateMetadata({ params }: { params: Promise<{ lang: Locale }> }) {
  const { lang } = await params;
  const dict = await getDictionary(lang);
  return {
    title: dict.meta.blog.title,
    description: dict.meta.blog.desc,
    alternates: {
      canonical: `/${lang}/blog`,
    },
  };
}

export default async function BlogPage({ params }: { params: Promise<{ lang: Locale }> }) {
  const { lang } = await params;
  const dict = await getDictionary(lang);
  
  const dbPosts = await getBlogPosts();
  
  const initialPosts = dbPosts.map((dbPost) => ({
    id: dbPost.id,
    slug: dbPost.slug,
    date: dbPost.date,
    author: dbPost.author,
    authorRole: dbPost.authorRole,
    image: dbPost.image,
    tr: {
      title: dbPost.trTitle,
      excerpt: dbPost.trExcerpt,
      readingTime: dbPost.trReadingTime,
      category: dbPost.trCategory,
      tags: dbPost.trTags,
      content: dbPost.trContent,
    },
    en: {
      title: dbPost.enTitle,
      excerpt: dbPost.enExcerpt,
      readingTime: dbPost.enReadingTime,
      category: dbPost.enCategory,
      tags: dbPost.enTags,
      content: dbPost.enContent,
    }
  }));

  return <BlogClient lang={lang} dict={dict} initialPosts={initialPosts} />;
}
