import {ConfigurableNodeDefinition, NodeType, NodeCategory, defineNodeOutputs} from './types';

/**
 * HTTP请求节点定义
 * 用于发送HTTP/HTTPS请求并解析响应，支持REST API调用、Webhook触发等场景
 */
export const HttpRequestNodeDefinition: ConfigurableNodeDefinition = {
    nodeCode: "HTTP_REQUEST",
    nodeName: "HTTP请求",
    nodeType: NodeType.HTTP_REQUEST,
    category: NodeCategory.TASK,
    description: "发送HTTP/HTTPS请求并解析响应",

    // 渲染配置
    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: "HTTP请求配置",
        description: "配置HTTP请求的URL、方法、参数等",
        properties: {
            continueOnFailure: {
                type: "boolean",
                title: "失败后继续",
                description: "当节点执行失败时，是否继续执行后续节点。true: 节点失败时标记为 FAILURE，但流程继续执行后续节点；false: 节点失败时抛出 BpmnError，终止流程",
                default: false
            },
            url: {
                type: "string",
                title: "请求URL",
                description: "HTTP请求的完整URL地址，支持变量表达式，如：${apiUrl}/status",
                "x-allow-variable": true,
                pattern: "^https?://.*",
                minLength: 1
            },
            method: {
                type: "string",
                title: "HTTP方法",
                description: "选择HTTP请求方法",
                enum: ["GET", "POST", "PUT", "DELETE", "PATCH"],
                enumNames: ["GET", "POST", "PUT", "DELETE", "PATCH"],
                default: "GET"
            },
            headers: {
                type: "array",
                title: "请求头",
                description: "自定义HTTP请求头，例如：Authorization, Content-Type",
                "x-component": "KeyValueEditor",
                items: {
                    type: "object",
                    properties: {
                        key: {
                            type: "string",
                            title: "Header名称"
                        },
                        value: {
                            type: "string",
                            title: "Header值"
                        }
                    },
                    required: ["key", "value"]
                },
                default: []
            },
            queryParams: {
                type: "array",
                title: "查询参数",
                description: "URL查询参数，会自动拼接到URL后面",
                "x-component": "KeyValueEditor",
                items: {
                    type: "object",
                    properties: {
                        key: {
                            type: "string",
                            title: "参数名"
                        },
                        value: {
                            type: "string",
                            title: "参数值"
                        }
                    },
                    required: ["key", "value"]
                },
                default: []
            },
            body: {
                type: "string",
                title: "请求体（JSON）",
                description: "HTTP请求体内容，支持变量表达式",
                format: "textarea",
                "x-allow-variable": true,
                "x-condition": {
                    field: "method",
                    operator: "in",
                    value: ["POST", "PUT", "PATCH"]
                }
            },
            timeout: {
                type: "number",
                title: "超时时间（毫秒）",
                description: "请求超时时间，0表示不限制",
                minimum: 1000,
                default: 30000
            },
            responseBodyType: {
                type: "string",
                title: "响应体类型",
                description: "响应内容的解析方式",
                enum: ["JSON", "TEXT", "XML", "BINARY"],
                enumNames: ["JSON", "纯文本", "XML", "二进制"],
                default: "JSON"
            },
            verifySsl: {
                type: "boolean",
                title: "SSL证书验证",
                description: "是否验证HTTPS证书，开发环境可关闭",
                default: true
            },
            followRedirects: {
                type: "boolean",
                title: "跟随重定向",
                description: "是否自动跟随HTTP重定向（3xx状态码）",
                default: true
            }
        },
        required: ["url", "method"]
    },

    // 输出字段定义
    outputs: defineNodeOutputs(
        {
            name: "statusCode",
            title: "HTTP状态码",
            type: "number",
            description: "响应的HTTP状态码（如 200, 404, 500）",
            example: 200,
            required: true
        },
        {
            name: "isSuccess",
            title: "是否成功",
            type: "boolean",
            description: "状态码为2xx时返回true",
            example: true,
            required: true
        },
        {
            name: "responseBody",
            title: "响应体",
            type: "object",
            description: "响应内容，JSON格式会自动解析为对象",
            example: { status: 'success', data: {} },
            required: false
        },
        {
            name: "responseTime",
            title: "响应时间（毫秒）",
            type: "number",
            description: "请求耗时（毫秒）",
            example: 150,
            required: true
        },
        {
            name: "errorMessage",
            title: "错误信息",
            type: "string",
            description: "失败时的错误描述",
            example: "Connection timeout",
            required: false
        },
        {
            name: "responseHeaders",
            title: "响应头",
            type: "object",
            description: "HTTP响应头信息",
            example: { "content-type": "application/json" },
            required: false
        }
    )
};
