'use client';

import type { ColumnDef } from '@tanstack/react-table';
import { DataTableColumnHeader } from '@/components/ui/table/data-table-column-header';
import { Badge } from '@/components/ui/badge';
import { CRM_MASTER_OPTION_CATEGORIES } from '@/features/foundation/master-options/types';
import type { MasterOptionItem } from '../api/types';

const categoryOptions = CRM_MASTER_OPTION_CATEGORIES.map((category) => ({
  label: category,
  value: category
}));

export const columns: ColumnDef<MasterOptionItem>[] = [
  {
    id: 'category',
    accessorKey: 'category',
    header: ({ column }) => <DataTableColumnHeader column={column} title='Category' />,
    meta: {
      label: 'Category',
      variant: 'select',
      options: categoryOptions
    },
    enableColumnFilter: true
  },
  {
    id: 'code',
    accessorKey: 'code',
    header: ({ column }) => <DataTableColumnHeader column={column} title='Code' />
  },
  {
    id: 'name',
    accessorKey: 'label',
    header: ({ column }) => <DataTableColumnHeader column={column} title='Label' />,
    meta: {
      label: 'Label',
      variant: 'text',
      placeholder: 'Search options...'
    },
    enableColumnFilter: true
  },
  {
    id: 'parent_label',
    accessorKey: 'parent_label',
    header: ({ column }) => <DataTableColumnHeader column={column} title='Parent' />,
    cell: ({ row }) => row.original.parent_label ?? '-'
  },
  {
    id: 'sort_order',
    accessorKey: 'sort_order',
    header: ({ column }) => <DataTableColumnHeader column={column} title='Sort' />
  },
  {
    id: 'is_active',
    accessorKey: 'is_active',
    header: ({ column }) => <DataTableColumnHeader column={column} title='Status' />,
    cell: ({ row }) =>
      row.original.is_active ? <Badge>Active</Badge> : <Badge variant='outline'>Inactive</Badge>
  }
];
