import type { Metadata } from "next";
import { CategoryListingPage } from "@/components/category-listing-page";
import { getCategory, type BlogCategory } from "@/lib/blog";
import { getCategories, type Category } from "@/lib/strapi";
import { WEDIGTECH_ICON_URL } from "@/lib/constants";

export const revalidate = 60; // ISR

export async function generateStaticParams() {
  // Dynamically fetch all categories from Strapi
  const categories = await getCategories() as Category[];

  // Strapi v5: slug is flat on the entity. De-duplicate since the dataset
  // can contain multiple categories sharing the same slug.
  const slugs = Array.from(
    new Set(categories.map((cat: Category) => cat?.slug).filter(Boolean))
  );

  return slugs.map((category) => ({ category }));
}

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

  if (!meta) {
    return { title: "Not Found" };
  }

  const title = `${meta.title} — Resources | wedigtech`;
  const description = meta.subtitle;

  return {
    title,
    description,
    openGraph: {
      title,
      description,
      images: [{ url: WEDIGTECH_ICON_URL }],
    },
    twitter: {
      title,
      description,
      images: ["https://wedigtech-strapi-media.s3.ap-south-1.amazonaws.com/files/icon.png"],
    },
  };
}

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