import { Layers } from "lucide-react";

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

import { parseStructureMasterSearchParams } from "./_components/constants";
import { StructureMasterHeaderActions } from "./_components/list-header-actions";
import { StructureMasterTable } from "./_components/structure-master-table";
import { fetchBackendStructureMaster } from "./_lib/structure-master-server-api";

type PageProps = {
  searchParams: Promise<{
    isin?: string;
    name?: string;
    duplicate_coupon_date?: string;
    duplicate_observation_date?: string;
    page?: string;
    page_size?: string;
  }>;
};

export default async function StructureMasterPage({ searchParams }: PageProps) {
  const params = await searchParams;
  const query = parseStructureMasterSearchParams(params);
  const { rows, detailsById, duplicateStats, totalCount, page, pageSize, pageCount, errorMessage } =
    await fetchBackendStructureMaster(query);

  return (
    <ListPageCard
      icon={Layers}
      title="View structure master"
      description="Structured product master records with coupon and observation date tracking."
      breadcrumb={[{ label: "Master Table" }, { label: "Structure master" }]}
      actions={<StructureMasterHeaderActions />}
    >
      <ErrorBanner message={errorMessage} className="mb-4" />
      <StructureMasterTable
        data={rows}
        filters={query}
        detailsById={detailsById}
        duplicateStats={duplicateStats}
        totalCount={totalCount}
        page={page}
        pageSize={pageSize}
        pageCount={pageCount}
      />
    </ListPageCard>
  );
}
