import type { ColumnDef } from '@tanstack/react-table';
import { Pencil, Trash2 } from 'lucide-react';
import { RowActions } from '@/components/data-display';

export type Category = {
    id: number;
    name: string;
};

type CreateColumnsParams = {
    handleEdit: (category: Category) => void;
    handleDeleteClick: (category: Category) => void;
    can: (permission: string) => boolean;
};

export function createCategoryColumns(
    params: CreateColumnsParams,
): ColumnDef<Category>[] {
    const { handleEdit, handleDeleteClick, can } = params;

    const columns: ColumnDef<Category>[] = [
        {
            accessorKey: 'name',
            header: () => <span>Nama</span>,
            cell: ({ row }) => (
                <span className="font-medium">
                    {row.getValue('name') as string}
                </span>
            ),
        },
    ];

    if (can('categories.update') || can('categories.delete')) {
        columns.push({
            id: 'actions',
            header: () => <span className="block text-center">Aksi</span>,
            meta: {
                className: 'w-[100px] text-center',
                headerClassName: 'w-[100px] text-center',
            },
            cell: ({ row }) => {
                const category = row.original;

                return (
                    <RowActions
                        actions={[
                            {
                                label: 'Edit',
                                icon: <Pencil className="h-4 w-4" />,
                                show: can('categories.update'),
                                onClick: () => handleEdit(category),
                            },
                            {
                                label: 'Hapus',
                                icon: (
                                    <Trash2 className="h-4 w-4 text-destructive" />
                                ),
                                show: can('categories.delete'),
                                onClick: () => handleDeleteClick(category),
                            },
                        ]}
                    />
                );
            },
        });
    }

    return columns;
}
