import { Metadata } from 'next';
import { getTranslations } from 'next-intl/server';

import { IoHomeOutline } from 'react-icons/io5';

import { getCreatorList } from '@/services/creators';

import Breadcrumbs, { BreadcrumbItem } from '@/components/common/Breadcrumbs';
import { getMetaId, metaAttributes110 } from '@/utils/filterUtils';

import CreatorListClient from '@/app/[locale]/alkotok/CreatorListClient';

export async function generateStaticParams() {
  return metaAttributes110.map((category) => ({
    category: category.prettyKey,
  }));
}

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

  // Find category info
  const categoryInfo = metaAttributes110.find((cat) => cat.prettyKey === category);

  const tCategory = await getTranslations('categories');
  const tMeta = await getTranslations('creatorListPage.metadata');

  const categoryName = categoryInfo ? tCategory(categoryInfo.labelKey) : tMeta('title');
  const description = tMeta('categoryDescriptionTemplate', { categoryName });
  const canonical = `/alkotok/${category}`;

  return {
    title: categoryName,
    description,
    alternates: {
      canonical,
    },
    openGraph: {
      url: canonical,
      title: categoryName,
      description,
      type: 'website',
    },
  };
}

export default async function CreatorListPage({
  params,
}: {
  params: Promise<{ category: string; locale: string }>;
}) {
  const { category, locale } = await params;
  const id = getMetaId(110, category);

  if (!id) return null;

  const creators = await getCreatorList({
    body: {
      metaIdList: [id],
    },
  });

  const t = await getTranslations('navigation.menu');
  const tCategory = await getTranslations('categories');
  const categoryInfo = metaAttributes110.find((cat) => cat.prettyKey === category);
  const categoryName = categoryInfo ? tCategory(categoryInfo.labelKey) : t('creators');

  const breadcrumbItems = [
    {
      label: locale === 'en' ? 'Home' : 'Főoldal',
      href: { pathname: '/' },
      icon: <IoHomeOutline />,
      ariaLabel: locale === 'en' ? 'Home' : 'Főoldal',
    },
    {
      label: t('creators'),
      href: { pathname: '/alkotok' },
    },
    {
      label: categoryName,
    },
  ] satisfies BreadcrumbItem[];

  return (
    <>
      <Breadcrumbs items={breadcrumbItems} className="px-site" />
      <CreatorListClient title={t('creators')} creators={creators} />
    </>
  );
}
