import {ConfigurableNodeDefinition, NodeType, NodeCategory, defineNodeOutputs} from './types';
import { DataSourceType } from '@/domain/dataSource';

/**
 * 审批节点定义
 * 用于人工审批流程，支持单人审批、多人会签、多人或签等模式
 */
export const ApprovalNodeDefinition: ConfigurableNodeDefinition = {
    nodeCode: "APPROVAL",
    nodeName: "审批节点",
    nodeType: NodeType.APPROVAL,
    category: NodeCategory.TASK,
    description: "人工审批节点，支持多种审批模式",

    // 渲染配置
    renderConfig: {
        shape: 'rounded-rect',
        size: {width: 140, height: 48},
        icon: {
            type: 'emoji',
            content: '✅',
            size: 32
        },
        theme: {
            primary: '#10b981',
            secondary: '#059669',
            selectedBorder: '#3b82f6',
            hoverBorder: '#10b981',
            gradient: ['#ffffff', '#d1fae5']
        },
        handles: {
            input: true,
            output: true
        },
        features: {
            showBadge: true,
            showHoverMenu: true
        }
    },

    // 输入配置Schema
    inputMappingSchema: {
        type: "object",
        title: "审批配置",
        description: "配置审批人和审批规则",
        properties: {
            approvalMode: {
                type: "string",
                title: "审批模式",
                description: "单人审批：仅需一人审批；会签：所有审批人都必须同意；或签：任一审批人同意即可",
                enum: ["SINGLE", "ALL", "ANY"],
                enumNames: ["单人审批", "会签（所有人同意）", "或签（任一人同意）"],
                default: "SINGLE"
            },
            approverType: {
                type: "string",
                title: "审批人类型",
                description: "指定审批人的方式：用户（选中的用户）、角色（拥有该角色的所有用户）、部门（该部门的所有成员）、变量（流程变量）",
                enum: ["USER", "ROLE", "DEPARTMENT", "VARIABLE"],
                enumNames: ["指定用户", "指定角色", "指定部门", "流程变量"],
                default: "USER"
            },
            approvers: {
                type: "array",
                title: "审批人列表",
                description: "单人审批：选择一个审批人；会签模式：选中的所有用户都必须审批；或签模式：任一用户审批即可",
                items: {
                    type: "string"  // 后台使用 username（字符串）
                },
                'x-dataSource': DataSourceType.USERS,
                'x-condition': {
                    field: 'approverType',
                    value: 'USER'
                },
                'x-multiple-condition': {
                    field: 'approvalMode',
                    operator: 'in',
                    value: ['ALL', 'ANY']  // 只有会签和或签才是多选
                }
            },
            approverRoles: {
                type: "array",
                title: "审批角色",
                description: "单人审批：选择一个角色；会签模式：拥有选中角色的所有用户都必须审批；或签模式：任一拥有该角色的用户审批即可",
                items: {
                    type: "string"  // 使用 code（字符串）
                },
                'x-dataSource': DataSourceType.ROLES,
                'x-condition': {
                    field: 'approverType',
                    value: 'ROLE'
                },
                'x-multiple-condition': {
                    field: 'approvalMode',
                    operator: 'in',
                    value: ['ALL', 'ANY']
                }
            },
            approverDepartments: {
                type: "array",
                title: "审批部门",
                description: "单人审批：选择一个部门；会签模式：选中部门的所有成员都必须审批；或签模式：任一部门成员审批即可",
                items: {
                    type: "string"  // 使用 code（字符串）
                },
                'x-dataSource': DataSourceType.DEPARTMENTS,
                'x-condition': {
                    field: 'approverType',
                    value: 'DEPARTMENT'
                },
                'x-multiple-condition': {
                    field: 'approvalMode',
                    operator: 'in',
                    value: ['ALL', 'ANY']
                }
            },
            approverVariable: {
                type: "string",
                title: "审批人变量",
                description: "使用流程变量指定审批人（格式：${变量名}）",
                'x-condition': {
                    field: 'approverType',
                    value: 'VARIABLE'
                }
            },
            approvalTitle: {
                type: "string",
                title: "审批标题",
                description: "审批任务的标题，支持变量",
                default: "待审批：${工作流实例.title}"
            },
            approvalContent: {
                type: "string",
                title: "审批内容",
                description: "审批任务的详细说明，支持变量",
                'x-component': "textarea"
            },
            timeoutDuration: {
                type: "number",
                title: "超时时间（小时）",
                description: "审批超时时间，0表示不限制",
                default: 0,
                minimum: 0
            },
            timeoutAction: {
                type: "string",
                title: "超时处理",
                description: "审批超时后的处理方式",
                enum: ["NONE", "AUTO_APPROVE", "AUTO_REJECT", "NOTIFY"],
                enumNames: ["无操作", "自动通过", "自动拒绝", "仅通知"],
                default: "NONE",
                'x-condition': {
                    field: 'timeoutDuration',
                    operator: '>',
                    value: 0
                }
            },
            allowDelegate: {
                type: "boolean",
                title: "允许转交",
                description: "是否允许审批人转交给其他人",
                default: false
            },
            allowAddSign: {
                type: "boolean",
                title: "允许加签",
                description: "是否允许审批人加签",
                default: false
            },
            requireComment: {
                type: "boolean",
                title: "必须填写意见",
                description: "审批时是否必须填写审批意见",
                default: false
            }
        },
        required: ["approvalMode", "approverType", "approvalTitle"]
    },

    // 输出配置
    outputs: defineNodeOutputs(
        {
            name: "approvalResult",
            title: "审批结果",
            type: "string",
            enum: ["APPROVED", "REJECTED", "TIMEOUT"],
            description: "审批的最终结果",
            example: "APPROVED",
            required: true
        },
        {
            name: "approver",
            title: "审批人",
            type: "string",
            description: "实际执行审批的用户",
            example: "张三",
            required: true
        },
        {
            name: "approverUserId",
            title: "审批人ID",
            type: "number",
            description: "审批人的用户ID",
            example: 1001,
            required: true
        },
        {
            name: "approvalTime",
            title: "审批时间",
            type: "string",
            description: "审批完成的时间",
            example: "2025-10-23T10:30:00Z",
            required: true
        },
        {
            name: "approvalComment",
            title: "审批意见",
            type: "string",
            description: "审批人填写的意见",
            example: "同意发布到生产环境",
            required: false
        },
        {
            name: "approvalDuration",
            title: "审批用时（秒）",
            type: "number",
            description: "从创建到完成审批的时长",
            example: 3600,
            required: true
        },
        {
            name: "allApprovers",
            title: "所有审批人",
            type: "array",
            description: "参与审批的所有人员列表（会签/或签时），格式：[{userId, userName, result, comment, time}]",
            example: [{userId: 1001, userName: "张三", result: "APPROVED", comment: "同意", time: "2025-10-23T10:30:00Z"}],
            required: false
        }
    )
};

// ✅ 不再需要单独的渲染组件，使用 BaseNode 即可

