"use client";

import * as React from "react";
import { DataTable } from "@/components/data-table/data-table";
import { Button } from "@/components/ui/button";
import { Plus, Loader2 } from "lucide-react";
import { useDataTable } from "@/hooks/use-data-table";
import { DataTableAdvancedToolbar } from "@/components/data-table/data-table-advanced-toolbar";
import type {
  DataTableAdvancedFilterField,
  DataTableFilterField,
  DataTableRowAction,
} from "@/types/table"
import { toast } from "sonner";
import { getColumns } from "./basic-contract-columns";
import { getBasicContracts, getBasicContractsByVendorId } from "../service";
import { BasicContractView } from "@/db/schema";
import { BasicContractTableToolbarActions } from "./basicContract-table-toolbar-actions";


interface BasicTemplateTableProps {
  promises: Promise<
    [
      Awaited<ReturnType<typeof getBasicContractsByVendorId>>,
    ]
  >
}


export function BasicContractsVendorTable({ promises }: BasicTemplateTableProps) {


  const [rowAction, setRowAction] =
    React.useState<DataTableRowAction<BasicContractView> | null>(null)


  const [{ data, pageCount }] =
    React.use(promises)

    // console.log(data)

  // 컬럼 설정 - 외부 파일에서 가져옴
  const columns = React.useMemo(
    () => getColumns({ setRowAction }),
    [setRowAction]
  )

  // config 기반으로 필터 필드 설정
  const advancedFilterFields: DataTableAdvancedFilterField<BasicContractView>[] = [
    { id: "templateName", label: "템플릿명", type: "text" },
    {
      id: "status", label: "상태", type: "select", options: [
        { label: "서명대기", value: "PENDING" },
        { label: "서명완료", value: "COMPLETED" },
      ]
    },
    { id: "userName", label: "요청자", type: "text" },
    { id: "createdAt", label: "생성일", type: "date" },
    { id: "updatedAt", label: "수정일", type: "date" },
  ];

  const { table } = useDataTable({
    data,
    columns,
    pageCount,
    // filterFields,
    enablePinning: true,
    enableAdvancedFilter: true,
    initialState: {
      sorting: [{ id: "createdAt", desc: true }],
      columnPinning: { right: ["actions"] },
    },
    getRowId: (originalRow) => String(originalRow.id),
    shallow: false,
    clearOnDefault: true,
  })

  return (
    <>

      <DataTable table={table}>
        <DataTableAdvancedToolbar
          table={table}
          filterFields={advancedFilterFields}
        >
          <BasicContractTableToolbarActions table={table} />

        </DataTableAdvancedToolbar>
      </DataTable>

    </>

  );
} 