import { ChartCandlestick } from "lucide-react";

import { ListPageCard } from "@/app/dashboard/_components";
import { ErrorBanner } from "@/components/shared/error-banner";

import { DerivativeMasterTable } from "./_components/derivative-master-table";
import { parseDerivativeMasterSearchParams } from "./_components/constants";
import { DerivativeMasterHeaderActions } from "./_components/list-header-actions";
import { fetchBackendDerivativeMaster } from "./_lib/derivative-master-server-api";

type PageProps = {
  searchParams: Promise<{
    parent_isin?: string;
    currency?: string;
    market_price?: string;
    page?: string;
    page_size?: string;
  }>;
};

export default async function DerivativeMasterPage({ searchParams }: PageProps) {
  const query = parseDerivativeMasterSearchParams(await searchParams);
  const { rows, totalCount, page, pageSize, pageCount, errorMessage } =
    await fetchBackendDerivativeMaster(query);

  return (
    <ListPageCard
      icon={ChartCandlestick}
      title="Derivative Master"
      description="Derivative instrument master data by parent ISIN."
      breadcrumb={[{ label: "Master Table" }, { label: "Derivative Master" }]}
      actions={<DerivativeMasterHeaderActions />}
    >
      <ErrorBanner message={errorMessage} className="mb-4" />
      <DerivativeMasterTable
        data={rows}
        filters={{
          parentIsin: query.parentIsin,
          currency: query.currency,
          marketPrice: query.marketPrice,
        }}
        totalCount={totalCount}
        page={page}
        pageSize={pageSize}
        pageCount={pageCount}
      />
    </ListPageCard>
  );
}
