import React from "react";
import { getBrands } from "@/actions/catalog";
import { BrandsOverviewClient } from "./BrandsOverviewClient";
import { BrandsOverview } from "./columns";

export default async function BrandsPage({
  searchParams,
}: {
  searchParams: Promise<{ [key: string]: string | string[] | undefined }>;
}) {
  const params = await searchParams;
  const page = typeof params["page"] === "string" ? parseInt(params["page"], 10) || 1 : 1;
  const pageSize = typeof params["pageSize"] === "string" ? parseInt(params["pageSize"], 10) || 10 : 10;

  const response = await getBrands({ limit: pageSize, page });

  const initialData = {
    documents: (response.success ? response.data.documents : []) as BrandsOverview[],
    total: response.success ? response.data.pagination.total : 0,
  };

  return (
    <BrandsOverviewClient
      initialData={initialData}
      page={page}
      pageSize={pageSize}
    />
  );
}
