"use client"

import * as React from "react"
import type {
  DataTableAdvancedFilterField,
  DataTableFilterField,
  DataTableRowAction,
} from "@/types/table"

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 { useFeatureFlags } from "./feature-flags-provider"

import { getColumns } from "./projects-table-columns"
import { getProjectLists } from "../service"
import { Project } from "@/db/schema"
import { ProjectTableToolbarActions } from "./projects-table-toolbar-actions"
import { ProjectDetailDialog } from "@/lib/projects/dialog/project-detail-dialog"

interface ItemsTableProps {
  promises: Promise<
    [
      Awaited<ReturnType<typeof getProjectLists>>,
    ]
  >
}

export function ProjectsTable({ promises }: ItemsTableProps) {
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const { featureFlags } = useFeatureFlags()

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

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const [rowAction, setRowAction] =
    React.useState<DataTableRowAction<Project> | null>(null)

  // 상세보기 다이얼로그 상태 관리
  const [selectedProject, setSelectedProject] = React.useState<Project | null>(null)
  const [dialogOpen, setDialogOpen] = React.useState(false)

  const handleDetailClick = React.useCallback((project: Project) => {
    setSelectedProject(project)
    setDialogOpen(true)
  }, [])

  const columns = React.useMemo(
    () => getColumns({ setRowAction, onDetailClick: handleDetailClick }),
    [setRowAction, handleDetailClick]
  )

  /**
   * This component can render either a faceted filter or a search filter based on the `options` prop.
   *
   * @prop options - An array of objects, each representing a filter option. If provided, a faceted filter is rendered. If not, a search filter is rendered.
   *
   * Each `option` object has the following properties:
   * @prop {string} label - The label for the filter option.
   * @prop {string} value - The value for the filter option.
   * @prop {React.ReactNode} [icon] - An optional icon to display next to the label.
   * @prop {boolean} [withCount] - An optional boolean to display the count of the filter option.
   */
  const filterFields: DataTableFilterField<Project>[] = [

  ]

  /**
   * Advanced filter fields for the data table.
   * These fields provide more complex filtering options compared to the regular filterFields.
   *
   * Key differences from regular filterFields:
   * 1. More field types: Includes 'text', 'multi-select', 'date', and 'boolean'.
   * 2. Enhanced flexibility: Allows for more precise and varied filtering options.
   * 3. Used with DataTableAdvancedToolbar: Enables a more sophisticated filtering UI.
   * 4. Date and boolean types: Adds support for filtering by date ranges and boolean values.
   */
  const advancedFilterFields: DataTableAdvancedFilterField<Project>[] = [
    {
      id: "code",
      label: "Project Code",
      type: "text",
      // group: "Basic Info",
    },
    {
      id: "name",
      label: "Project Name",
      type: "text",
      // group: "Basic Info",
    },
   
  
    {
      id: "createdAt",
      label: "Created At",
      type: "date",
      // group: "Metadata",a
    },
    {
      id: "updatedAt",
      label: "Updated At",
      type: "date",
      // group: "Metadata",
    },
  ]


  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}
          shallow={false}
        >
          <ProjectTableToolbarActions table={table} />
        </DataTableAdvancedToolbar>
      </DataTable>

      {/* 프로젝트 상세보기 다이얼로그 */}
      {selectedProject && (
        <ProjectDetailDialog
          project={selectedProject}
          open={dialogOpen}
          onOpenChange={setDialogOpen}
        />
      )}
    </>
  )
}
