import type { ColumnDef } from '@tanstack/react-table'

import { MenuIcon } from '@/components/menu-icon'
import { menuTypeDict, statusDict, type MenuTypeValues, type StatusValues } from '@/enum'
import { formatDate } from '@/lib'
import type { SysMenuTree } from '@/schemas'

import { RowActions } from './row-actions.tsx'

export const createColumns = (): ColumnDef<SysMenuTree>[] => {
  return [
    {
      accessorKey: 'name',
      header: '菜单名称',
      meta: { className: 'min-w-36 max-w-36' },
      cell: ({ row }) => (
        <div className='overflow-hidden flex gap-2 items-center'>
          {row.original.icon && (
            <MenuIcon className='size-4 text-gray-500' menuIconKey={row.original.icon} />
          )}
          {row.getValue('name')}
        </div>
      ),
    },
    {
      accessorKey: 'path',
      header: '菜单路径',
      meta: { className: ' max-w-36' },
      cell: ({ row }) => <div className='overflow-hidden'>{row.getValue('path')}</div>,
    },
    {
      accessorKey: 'type',
      header: '菜单类型',
      meta: { className: 'max-w-20' },
      cell: ({ row }) => {
        const menuType = menuTypeDict[row.getValue('type') as MenuTypeValues]
        return <div>{menuType}</div>
      },
    },
    {
      accessorKey: 'permission_code',
      header: '权限字符',
      meta: { className: ' max-w-36' },
      cell: ({ row }) => <div>{row.getValue('permission_code')}</div>,
    },
    {
      accessorKey: 'status',
      header: '菜单状态',
      meta: { className: ' max-w-36' },
      cell: ({ row }) => {
        const status = statusDict[row.getValue('status') as StatusValues]
        return <div>{status}</div>
      },
    },
    {
      accessorKey: 'hidden',
      header: '是否隐藏',
      meta: { className: ' max-w-36' },
      cell: ({ row }) => <div>{row.getValue('hidden') ? '是' : '否'}</div>,
    },
    {
      accessorKey: 'sort',
      header: '菜单排序',
      meta: { className: ' max-w-36' },
      cell: ({ row }) => <div>{row.getValue('sort')}</div>,
    },
    {
      accessorKey: 'created_at',
      header: '创建时间',
      meta: { className: ' max-w-36' },
      cell: ({ row }) => {
        const rawDate = row.getValue('created_at') as string
        return <div className='overflow-hidden'>{formatDate(rawDate)}</div>
      },
    },
    {
      accessorKey: 'updated_at',
      header: '修改时间',
      meta: { className: ' max-w-36' },
      cell: ({ row }) => {
        const rawDate = row.getValue('updated_at') as string
        return <div className='overflow-hidden'>{formatDate(rawDate)}</div>
      },
    },
    {
      id: 'actions',
      enableHiding: false,
      meta: { className: ' max-w-36 sticky right-0' },
      cell: RowActions,
    },
  ]
}
