"use client";

import { CustomerStandardAssetListTable } from "@/app/customer/_components/customer-standard-asset-list-table";
import { navigateToDerivativeChildEdit } from "@/app/customer/_lib/derivative-edit-navigation";

import { StockDetailSheet } from "../../../stock/_components/list-stocks-table/stock-detail-sheet";
import {
  createStockColumns,
  FALLBACK_STOCK_GRID_COLUMNS,
  STOCK_OPTIONS_COLUMN_ID,
} from "../../../stock/_components/list-stocks-table/columns";
import {
  buildStockPageSearchParams,
  DEFAULT_STOCK_PAGE_SIZE,
  DEFAULT_STOCK_SORTING,
  EMPTY_STOCK_FILTERS,
  getStockFilterColumnValue,
  parseStockPageStateFromSearchParams,
  placementDateRangeToStockFilters,
  setStockFilterColumnValue,
} from "../../../stock/_lib/stock-filters";
import { saveDerivativesFixedIncomeColumnPrefsClient } from "../../_lib/derivatives-fixed-income-api";
import { useDerivativesFixedIncomeList } from "../../_lib/use-derivatives-fixed-income-list";
import { resolveDerivativeEditTarget } from "../../_lib/parse-legacy-derivative-edit-url";
import { DerivativesCreateMenu } from "../derivatives-create-menu";
import { usePortfolioScopePageReset } from "@/app/customer/_lib/admin/use-portfolio-scope-page-reset";

export function ListDerivativesFixedIncomeTable() {
  return (
    <CustomerStandardAssetListTable
      title="List Derivatives"
      emptyMessage="No derivatives found."
      rowsPerPageId="derivatives-fixed-income-rows-per-page"
      singularName="Derivative"
      // No moduleId and no formConfig: this hub owns neither a form nor a
      // delete/update module — rows belong to the bond accumulator children,
      // and wrapOnEdit routes into their forms.
      listPath="/customer/derivatives-fixed-income"
      optionsColumnId={STOCK_OPTIONS_COLUMN_ID}
      fallbackColumns={FALLBACK_STOCK_GRID_COLUMNS}
      defaultPageSize={DEFAULT_STOCK_PAGE_SIZE}
      defaultSorting={DEFAULT_STOCK_SORTING}
      emptyFilters={EMPTY_STOCK_FILTERS}
      useList={useDerivativesFixedIncomeList}
      saveColumnPrefs={saveDerivativesFixedIncomeColumnPrefsClient}
      parsePageState={parseStockPageStateFromSearchParams}
      buildPageSearchParams={buildStockPageSearchParams}
      getFilterColumnValue={getStockFilterColumnValue}
      setFilterColumnValue={setStockFilterColumnValue}
      placementDateRangeToFilters={placementDateRangeToStockFilters}
      createColumns={(catalog, onView, onEdit, options) =>
        createStockColumns(catalog, onView, onEdit, {
          priceDecimals: 2,
          quantityUseLocale: true,
          listPath: options.listPath,
          pagePermissions: options.pagePermissions,
        })
      }
      wrapOnEdit={(row, _defaultOnEdit, ctx) =>
        navigateToDerivativeChildEdit(resolveDerivativeEditTarget(row), row, ctx)
      }
      DetailSheet={StockDetailSheet}
      showNewButton={false}
      resolveCanCreate={(permissions) =>
        permissions.bond_accumulator_create === true ||
        permissions.bond_funds_accumulator_create === true
      }
      toolbarActions={({ listPath, permissions }) =>
        permissions.bond_accumulator_create === true ||
        permissions.bond_funds_accumulator_create === true ? (
          <DerivativesCreateMenu listPath={listPath} permissions={permissions} />
        ) : null
      }
    />
  );
}
