import { products } from '@/data/products';
import { categoryNameMap, CategorySlug } from '@/types';
import { filterProducts, parseSearchParams } from '@/lib/filterProducts';
import { CatalogView } from '@/components/CatalogView/CatalogView';

export async function generateMetadata({
  searchParams,
}: {
  searchParams: Promise<Record<string, string | string[] | undefined>>;
}) {
  const params = await searchParams;
  const category = params.category as string | undefined;
  const catName = category
    ? categoryNameMap[category as CategorySlug]
    : undefined;

  const title = catName
    ? `${catName} | Каталог | PAN-PROM`
    : 'Каталог | PAN-PROM';

  return { title };
}

export default async function CatalogPage({
  searchParams,
}: {
  searchParams: Promise<Record<string, string | string[] | undefined>>;
}) {
  const params = await searchParams;
  const filters = parseSearchParams(params);
  const filtered = filterProducts(products, filters);

  const breadcrumbItems = [
    { label: 'Главная', href: '/' },
    { label: 'Каталог' },
  ];

  return (
    <CatalogView products={filtered} breadcrumbItems={breadcrumbItems} />
  );
}
