"use client";

import * as React from "react";

import { useDataTable } from "@/hooks/use-data-table";
import { DataTable } from "@/components/data-table/data-table";
import { DataTableAdvancedToolbar } from "@/components/data-table/data-table-advanced-toolbar";

import { getColumns } from "./possible-items-table-columns";
import { PossibleItemsTableToolbarActions } from "./possible-items-table-toolbar-actions";

// 타입만 import
type TechVendorPossibleItemsData = {
  id: number;
  vendorId: number;
  vendorCode: string | null;
  vendorName: string;
  techVendorType: string;
  itemCode: string;
  itemList: string | null;
  workType: string | null;
  shipTypes: string | null;
  subItemList: string | null;
  createdAt: Date;
  updatedAt: Date;
};
import type { DataTableAdvancedFilterField } from "@/types/table";

interface PossibleItemsDataTableProps {
  promises: Promise<[{
    data: TechVendorPossibleItemsData[];
    pageCount: number;
    totalCount: number;
  }, string[]]>;
}

export function PossibleItemsDataTable({ promises }: PossibleItemsDataTableProps) {
  const [{ data, pageCount }, vendorTypes] = React.use(promises);

  const columns = React.useMemo(() => getColumns(), []);

  const filterFields: DataTableAdvancedFilterField<TechVendorPossibleItemsData>[] = [
    {
      id: "vendorCode",
      label: "벤더코드",
      type: "text",
    },
    {
      id: "vendorName",
      label: "벤더명", 
      type: "text",
    },
    {
      id: "itemCode",
      label: "아이템코드",
      type: "text",
    },
    {
      id: "itemList",
      label: "아이템리스트",
      type: "text",
    },
    {
      id: "workType",
      label: "공종",
      type: "text",
    },
    {
      id: "shipTypes",
      label: "선종",
      type: "text",
    },
    {
      id: "subItemList",
      label: "서브아이템리스트",
      type: "text",
    },
    {
      id: "techVendorType",
      label: "벤더타입",
      type: "multi-select",
      options: Array.isArray(vendorTypes) ? vendorTypes.map((type: string) => ({
        label: type,
        value: type,
        count: 0,
      })) : [],
    },
  ];

  const { table } = useDataTable({
    data,
    columns,
    pageCount,
    filterFields,
    enableAdvancedFilter: true,
    initialState: {
      sorting: [{ id: "createdAt", desc: true }],
      pagination: { pageIndex: 0, pageSize: 10 },
    },
    getRowId: (originalRow) => `${originalRow.vendorId}-${originalRow.itemCode}-${originalRow.id}`,
    shallow: false,
    clearOnDefault: true,
  });

  return (
    <>
      <DataTable table={table}>
        <DataTableAdvancedToolbar table={table} filterFields={filterFields}>
          <PossibleItemsTableToolbarActions table={table} />
        </DataTableAdvancedToolbar>
      </DataTable>
    </>
  );
} 