"use client";

import { mixedFundsFormConfig } from "@/components/form/transaction-workspace/module-configs";
import { CustomerStandardAssetListTable } from "@/app/customer/_components/customer-standard-asset-list-table";

import { saveMixedFundColumnPrefsClient } from "../../_lib/mixed-funds-api";
import { useMixedFundList } from "../../_lib/use-mixed-fund-list";
import { resolveMixedFundsEditTarget } from "../../_lib/resolve-mixed-funds-edit-target";
import {
  buildMixedFundPageSearchParams,
  DEFAULT_MIXED_FUND_PAGE_SIZE,
  DEFAULT_MIXED_FUND_SORTING,
  EMPTY_MIXED_FUND_FILTERS,
  getMixedFundFilterColumnValue,
  parseMixedFundPageStateFromSearchParams,
  placementDateRangeToMixedFundFilters,
  setMixedFundFilterColumnValue,
} from "../../_lib/mixed-funds-filters";
import { MixedFundsCreateMenu } from "../mixed-funds-create-menu";
import {
  createMixedFundColumns,
  FALLBACK_MIXED_FUND_GRID_COLUMNS,
  MIXED_FUND_OPTIONS_COLUMN_ID,
} from "./columns";
import { MixedFundDetailSheet } from "./mixed-funds-detail-sheet";

function useMixedFundListForStandardTable(
  args: Parameters<typeof useMixedFundList>[0],
) {
  const list = useMixedFundList(args);
  return {
    ...list,
    bankOptions: list.filterOptions.banks,
    typeOptions: list.filterOptions.types,
    executionTypeOptions: list.filterOptions.executionTypes,
    transactionOptions: list.filterOptions.transactions,
    filterOptions: undefined,
  };
}

export function ListMixedFundsTable() {
  return (
    <CustomerStandardAssetListTable
      title="List Mixed Funds"
      emptyMessage="No mixed funds found."
      rowsPerPageId="mixed-funds-rows-per-page"
      moduleId="mixed-funds"
      singularName="Mixed fund"
      formConfig={mixedFundsFormConfig}
      optionsColumnId={MIXED_FUND_OPTIONS_COLUMN_ID}
      fallbackColumns={FALLBACK_MIXED_FUND_GRID_COLUMNS}
      defaultPageSize={DEFAULT_MIXED_FUND_PAGE_SIZE}
      defaultSorting={DEFAULT_MIXED_FUND_SORTING}
      emptyFilters={EMPTY_MIXED_FUND_FILTERS}
      showNewButton={false}
      resolveCanCreate={(permissions) =>
        Boolean(permissions.create) ||
        Boolean(permissions.option_create) ||
        Boolean(permissions.accumulator_create)
      }
      toolbarActions={({ listPath, permissions }) => {
        const showCreateMenu =
          Boolean(permissions.create) ||
          Boolean(permissions.option_create) ||
          Boolean(permissions.accumulator_create);
        return showCreateMenu ? (
          <MixedFundsCreateMenu listPath={listPath} permissions={permissions as never} />
        ) : null;
      }}
      useList={useMixedFundListForStandardTable}
      saveColumnPrefs={saveMixedFundColumnPrefsClient}
      parsePageState={parseMixedFundPageStateFromSearchParams}
      buildPageSearchParams={buildMixedFundPageSearchParams}
      getFilterColumnValue={getMixedFundFilterColumnValue}
      setFilterColumnValue={setMixedFundFilterColumnValue}
      placementDateRangeToFilters={placementDateRangeToMixedFundFilters}
      createColumns={(catalog, onView, onEdit, options) =>
        createMixedFundColumns(catalog, onView, onEdit, {
          listPath: options.listPath,
          pagePermissions: options.pagePermissions as never,
        })
      }
      wrapOnEdit={(row, defaultOnEdit, { listPath, router }) => {
        const parsed = resolveMixedFundsEditTarget(row);
        if (parsed) {
          const params = new URLSearchParams({
            child: parsed.child,
            id: parsed.id,
          });
          if (parsed.pid) {
            params.set("pid", parsed.pid);
          }
          router.push(`${listPath}/form?${params.toString()}`);
          return;
        }
        defaultOnEdit(row);
      }}
      DetailSheet={MixedFundDetailSheet}
    />
  );
}
