import type { Metadata } from "next";
import { ArticlePage } from "@/components/article-page";
import { getAllSlugs, getPost, CATEGORIES, type BlogCategory } from "@/lib/blog";
import { WEDIGTECH_ICON_URL } from "@/lib/constants";

export const revalidate = 60; // ISR

export async function generateStaticParams() {
  const categories: BlogCategory[] = ["insights", "impact-stories"];
  const params = [];

  for (const category of categories) {
    const slugs = await getAllSlugs(category);
    for (const slug of slugs) {
      params.push({ category, slug: slug.slug });
    }
  }

  return params;
}

export async function generateMetadata({
  params,
}: {
  params: Promise<{ category: string; slug: string }>;
}): Promise<Metadata> {
  const { category, slug } = await params;
  const post = await getPost(category as BlogCategory, slug);

  if (!post) return { title: "Article not found | wedigtech" };

  const categoryMeta = CATEGORIES[category as BlogCategory];
  const categoryLabel = categoryMeta?.label || "Resource";

  const title = `${post.title} | wedigtech ${categoryLabel}`;
  const description = post.excerpt;

  return {
    title,
    description,
    openGraph: {
      title,
      description,
      images: [{ url: post.image || WEDIGTECH_ICON_URL }],
    },
    twitter: {
      title,
      description,
      images: [post.image || WEDIGTECH_ICON_URL],
    },
  };
}

export default async function ArticleDetailPage({
  params,
}: {
  params: Promise<{ category: string; slug: string }>;
}) {
  const { category, slug } = await params;
  return <ArticlePage category={category as BlogCategory} slug={slug} />;
}
