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

export type Role = {
    id: number;
    name: string;
    permissions_count: number;
};

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

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

    return [
        {
            accessorKey: 'name',
            header: () => <span>Nama Role</span>,
            cell: ({ row }) => (
                <span className="font-medium">
                    {row.getValue('name') as string}
                </span>
            ),
        },
        {
            accessorKey: 'permissions_count',
            header: () => (
                <span className="block text-center">Jumlah Permission</span>
            ),
            meta: {
                className: 'w-[180px] text-center',
                headerClassName: 'w-[180px] text-center',
            },
            cell: ({ row }) => (
                <span className="inline-flex items-center rounded-md bg-muted px-2 py-1 text-xs font-medium">
                    {row.getValue('permissions_count') as number} permission
                </span>
            ),
        },
        {
            id: 'actions',
            header: () => <span className="block text-center">Aksi</span>,
            meta: {
                className: 'w-[100px] text-center',
                headerClassName: 'w-[100px] text-center',
            },
            cell: ({ row }) => {
                const role = row.original;

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