import React from 'react';
import { Button, Space, Popconfirm } from 'antd';
import { Eye, Pencil, Trash2 } from 'lucide-react';
import { ICON_SIZE_MD } from '@/components/icons';
import type { ColumnsType } from 'antd/es/table';

interface Action<T = unknown> {
    key: string;
    label: string;
    icon?: React.ReactNode;
    onClick: (record: T) => void;
    danger?: boolean;
    hidden?: (record: T) => boolean;
    confirm?: {
        title: string;
        description?: string;
        okText?: string;
        cancelText?: string;
    };
}

interface ActionsColumnProps<T = unknown> {
    actions: Action<T>[];
    title?: string;
    width?: number | string;
    fixed?: 'left' | 'right';
}

export function createActionsColumn<T = unknown>({
    actions,
    title = 'Ações',
    width = 120,
    fixed,
}: ActionsColumnProps<T>): ColumnsType<T>[0] {
    return {
        title,
        key: 'actions',
        fixed,
        width,
        align: 'center',
        render: (_: unknown, record: T) => (
            <Space size="small">
                {actions
                    .filter((action) => !action.hidden || !action.hidden(record))
                    .map((action) => {
                        const button = (
                            <Button
                                key={action.key}
                                type={action.danger ? 'primary' : 'default'}
                                danger={action.danger}
                                icon={action.icon}
                                size="small"
                                onClick={() => action.onClick(record)}
                            >
                                {action.label}
                            </Button>
                        );

                        if (action.confirm) {
                            return (
                                <Popconfirm
                                    key={action.key}
                                    title={action.confirm.title}
                                    description={action.confirm.description}
                                    onConfirm={() => action.onClick(record)}
                                    okText={action.confirm.okText ?? 'Sim'}
                                    cancelText={action.confirm.cancelText ?? 'Cancelar'}
                                >
                                    {button}
                                </Popconfirm>
                            );
                        }

                        return button;
                    })}
            </Space>
        ),
    };
}

// Helpers para ações comuns
export const commonActions = {
    view: <T = unknown,>(onClick: (record: T) => void): Action<T> => ({
        key: 'view',
        label: 'Ver',
        icon: <Eye size={ICON_SIZE_MD} aria-hidden />,
        onClick,
    }),
    edit: <T = unknown,>(onClick: (record: T) => void): Action<T> => ({
        key: 'edit',
        label: 'Editar',
        icon: <Pencil size={ICON_SIZE_MD} aria-hidden />,
        onClick,
    }),
    delete: <T = unknown,>(
        onClick: (record: T) => void,
        title = 'Tem certeza que deseja excluir?'
    ): Action<T> => ({
        key: 'delete',
        label: 'Excluir',
        icon: <Trash2 size={ICON_SIZE_MD} aria-hidden />,
        danger: true,
        onClick,
        confirm: {
            title,
            description: 'Esta ação não pode ser desfeita.',
            okText: 'Sim, excluir',
            cancelText: 'Cancelar',
        },
    }),
};
