"use client"

import * as React from "react"
import { type DataTableRowAction } from "@/types/table"
import { type ColumnDef } from "@tanstack/react-table"
import { Ellipsis } from "lucide-react"


import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuShortcut,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"

import { DataTableColumnHeaderSimple } from "@/components/data-table/data-table-column-simple-header"

interface ComboBoxOption {
  id: number
  codeGroupId: number
  code: string
  description: string
  remark: string | null
  isActive?: boolean
  createdAt: Date
  updatedAt: Date
}

interface GetColumnsProps {
  setRowAction: React.Dispatch<React.SetStateAction<DataTableRowAction<ComboBoxOption> | null>>
}

/**
 * tanstack table 컬럼 정의
 */
export function getColumns({ setRowAction }: GetColumnsProps): ColumnDef<ComboBoxOption>[] {
  // ----------------------------------------------------------------
  // 1) select 컬럼 (체크박스)
  // ----------------------------------------------------------------
  const selectColumn: ColumnDef<ComboBoxOption> = {
    id: "select",
    header: ({ table }) => (
      <Checkbox
        checked={
          table.getIsAllPageRowsSelected() ||
          (table.getIsSomePageRowsSelected() && "indeterminate")
        }
        onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
        aria-label="Select all"
        className="translate-y-0.5"
      />
    ),
    cell: ({ row }) => (
      <Checkbox
        checked={row.getIsSelected()}
        onCheckedChange={(value) => row.toggleSelected(!!value)}
        aria-label="Select row"
        className="translate-y-0.5"
      />
    ),
    maxSize: 30,
    enableSorting: false,
    enableHiding: false,
  }

  // ----------------------------------------------------------------
  // 2) actions 컬럼 (Dropdown 메뉴)
  // ----------------------------------------------------------------
  const actionsColumn: ColumnDef<ComboBoxOption> = {
    id: "actions",
    enableHiding: false,
    cell: function Cell({ row }) {
      return (
        <DropdownMenu>
          <DropdownMenuTrigger asChild>
            <Button
              aria-label="Open menu"
              variant="ghost"
              className="flex size-8 p-0 data-[state=open]:bg-muted"
            >
              <Ellipsis className="size-4" aria-hidden="true" />
            </Button>
          </DropdownMenuTrigger>
          <DropdownMenuContent align="end" className="w-40">
            <DropdownMenuItem
              onSelect={() => setRowAction({ row, type: "update" })}
            >
              Edit
            </DropdownMenuItem>

            <DropdownMenuSeparator />
            <DropdownMenuItem
              onSelect={() => setRowAction({ row, type: "delete" })}
            >
              Delete
              <DropdownMenuShortcut>⌘⌫</DropdownMenuShortcut>
            </DropdownMenuItem>
          </DropdownMenuContent>
        </DropdownMenu>
      )
    },
    maxSize: 30,
  }

  // ----------------------------------------------------------------
  // 3) 데이터 컬럼들
  // ----------------------------------------------------------------
  const dataColumns: ColumnDef<ComboBoxOption>[] = [
    {
      accessorKey: "code",
      enableResizing: true,
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="코드" />
      ),
      meta: {
        excelHeader: "코드",
        type: "text",
      },
      cell: ({ row }) => row.getValue("code") ?? "",
      minSize: 80
    },
    {
      accessorKey: "description",
      enableResizing: true,
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="값" />
      ),
      meta: {
        excelHeader: "값",
        type: "text",
      },
      cell: ({ row }) => row.getValue("description") ?? "",
      minSize: 80
    },
    {
      accessorKey: "remark",
      enableResizing: true,
      header: ({ column }) => (
        <DataTableColumnHeaderSimple column={column} title="비고" />
      ),
      meta: {
        excelHeader: "비고",
        type: "text",
      },
      cell: ({ row }) => row.getValue("remark") ?? "",
      minSize: 80
    }
  ]

  // ----------------------------------------------------------------
  // 4) 최종 컬럼 배열: select, dataColumns, actions
  // ----------------------------------------------------------------
  return [
    selectColumn,
    ...dataColumns,
    actionsColumn,
  ]
} 