import type { Metadata } from "next";
import { CatalogClient } from "@/components/catalog/catalog-client";
import { getProducts } from "@/lib/headless/client";

export const metadata: Metadata = {
  title: "Catálogo",
  description: "Explora todos los productos de Ari Shopping.",
};

type CatalogPageProps = {
  searchParams: Promise<Record<string, string | string[] | undefined>>;
};

function single(value: string | string[] | undefined): string {
  return Array.isArray(value) ? value[0] ?? "" : value ?? "";
}

export default async function CatalogPage({ searchParams }: CatalogPageProps) {
  const params = await searchParams;
  const products = await getProducts();

  const category = single(params.category) || "Todos";
  const collection = single(params.collection);
  const query = single(params.q);
  const section = single(params.section);

  return (
    <CatalogClient
      key={`${category}:${collection}:${query}:${section}`}
      products={products}
      initialCategory={category}
      initialCollection={collection}
      initialQuery={query}
    />
  );
}
