"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 type { DataTableAdvancedFilterField } from "@/types/table"
import { getColumns } from "./combo-box-settings-table-columns"
import { ComboBoxOptionsDetailSheet } from "./combo-box-options-detail-sheet"
import { codeGroups } from "@/db/schema/docu-list-rule"

interface ComboBoxSettingsTableProps {
  promises?: Promise<[{ data: typeof codeGroups.$inferSelect[]; pageCount: number }]>
}

export function ComboBoxSettingsTable({ promises }: ComboBoxSettingsTableProps) {

  const rawData = React.use(promises!)
  const [isDetailSheetOpen, setIsDetailSheetOpen] = React.useState(false)
  const [selectedCodeGroup, setSelectedCodeGroup] = React.useState<typeof codeGroups.$inferSelect | null>(null)
  // Detail 버튼 클릭 핸들러
  const handleDetail = React.useCallback((codeGroup: typeof codeGroups.$inferSelect) => {
    setSelectedCodeGroup(codeGroup)
    setIsDetailSheetOpen(true)
  }, [])

  const columns = React.useMemo(() => getColumns({ onDetail: handleDetail }), [handleDetail])

  // 고급 필터 필드 설정
  const advancedFilterFields: DataTableAdvancedFilterField<typeof codeGroups.$inferSelect>[] = [
    { id: "groupId", label: "Group ID", type: "text" },
    { id: "description", label: "Description", type: "text" },
    { id: "codeFormat", label: "Code Format", type: "text" },
    {
      id: "controlType", label: "Control Type", type: "select", options: [
        { label: "Textbox", value: "textbox" },
        { label: "Combobox", value: "combobox" },
      ]
    },
    {
      id: "isActive", label: "Status", type: "select", options: [
        { label: "Active", value: "true" },
        { label: "Inactive", value: "false" },
      ]
    },
    { id: "createdAt", label: "Created At", type: "date" },
  ]

  const { table } = useDataTable({
    data: rawData[0].data as typeof codeGroups.$inferSelect[],
    columns,
    pageCount: rawData[0].pageCount,
    enablePinning: true,
    enableAdvancedFilter: true,
    initialState: {
      columnPinning: { right: ["actions"] },
      columnFilters: [
        {
          id: "controlType",
          value: "combobox",
        },
      ],
    },
    getRowId: (originalRow) => String(originalRow.groupId),
    shallow: false,
    clearOnDefault: true,
  })

  return (
    <>
      <DataTable table={table}>
        <DataTableAdvancedToolbar
          table={table}
          filterFields={advancedFilterFields}
        >
        
        </DataTableAdvancedToolbar>
      </DataTable>

      {/* Detail 시트 */}
      <ComboBoxOptionsDetailSheet
        open={isDetailSheetOpen}
        onOpenChange={setIsDetailSheetOpen}
        codeGroup={selectedCodeGroup}
        onSuccess={() => {
          setIsDetailSheetOpen(false)
          setSelectedCodeGroup(null)
        }}

      />


    </>
  )
} 