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

/**
 * Jenkins构建节点定义（纯配置）
 * 使用 BaseNode 组件渲染，无需自定义渲染代码
 */
export const JenkinsBuildNodeDefinition: ConfigurableNodeDefinition = {
    nodeCode: "JENKINS_BUILD",
    nodeName: "Jenkins构建",
    nodeType: NodeType.JENKINS_BUILD,
    category: NodeCategory.TASK,
    description: "通过Jenkins执行构建任务",

    // 渲染配置（配置驱动）
    renderConfig: {
        shape: 'rounded-rect',
        size: {width: 140, height: 48},
        icon: {
            type: 'emoji',
            content: '🔨',
            size: 32
        },
        theme: {
            primary: '#f59e0b',
            secondary: '#d97706',
            selectedBorder: '#3b82f6',
            hoverBorder: '#f59e0b',
            gradient: ['#ffffff', '#fef3c7']
        },
        handles: {
            input: true,
            output: true
        },
        features: {
            showBadge: true,
            showHoverMenu: true
        }
    },

    // 基本配置Schema（仅业务配置，元数据已移至固定表单）
    inputMappingSchema: {
        type: "object",
        title: "输入",
        description: "当前节点所需数据配置",
        properties: {
            continueOnFailure: {
                type: "boolean",
                title: "失败后继续",
                description: "当节点执行失败时，是否继续执行后续节点。true: 节点失败时标记为 FAILURE，但流程继续执行后续节点；false: 节点失败时抛出 BpmnError，终止流程",
                default: true
            },
            serverId: {
                type: "number",
                title: "Jenkins服务器",
                description: "选择Jenkins服务器或输入变量",
                "x-dataSource": DataSourceType.JENKINS_SERVERS,
                "x-allow-variable": true
            },
            jobName: {
                type: "string",
                title: "构建任务",
                description: "输入构建任务名称或输入变量",
                // 注意：jobName 暂时使用手动输入，因为需要先选择 serverId 才能级联加载 jobs
                // 未来如果需要级联下拉，需要使用 CascadeDataSourceType.JENKINS_SERVER_VIEWS_JOBS
                "x-allow-variable": true
            }
        },
        required: ["serverId", "jobName"]
    },
    outputs: defineNodeOutputs(
        {
            name: "buildStatus",
            title: "构建状态",
            type: "string",
            enum: ["SUCCESS", "FAILURE", "ABORTED", "NOT_FOUND"],
            description: "节点执行的最终状态",
            required: true
        },
        {
            name: "buildNumber",
            title: "构建编号",
            type: "number",
            description: "Jenkins构建的唯一编号",
            example: 123,
            required: true
        },
        {
            name: "buildUrl",
            title: "构建URL",
            type: "string",
            description: "Jenkins构建页面的访问地址",
            example: "http://jenkins.example.com/job/app/123/",
            required: true
        },
        {
            name: "artifactUrl",
            title: "构建产物地址",
            type: "string",
            description: "构建生成的jar/war包下载地址",
            example: "http://jenkins.example.com/job/app/123/artifact/target/app-1.0.0.jar",
            required: false
        },
        {
            name: "gitCommitId",
            title: "Git提交ID",
            type: "string",
            description: "本次构建使用的Git提交哈希值",
            example: "a3f5e8d2c4b1a5e9f2d3e7b8c9d1a2f3e4b5c6d7",
            required: true
        },
        {
            name: "buildDuration",
            title: "构建时长",
            type: "number",
            description: "构建执行的时长（秒）",
            example: 120,
            required: true
        },
        {
            name: "buildDurationMillis",
            title: "构建时长(毫秒)",
            type: "number",
            description: "构建执行的时长（毫秒）",
            example: 158000,
            required: false
        },
        {
            name: "buildDurationFormatted",
            title: "构建时长(格式)",
            type: "string",
            description: "构建执行的时长（格式：HH:mm:ss）",
            example: "00:02:39",
            required: false
        },
        {
            name: "buildEndTimeMillis",
            title: "部署结束时间(ms)",
            type: "number",
            description: "部署结束时间（毫秒）",
            example: 1700000000000,
            required: false
        },
        {
            name: "buildEndTime",
            title: "部署结束时间",
            type: "string",
            description: "部署结束时间（格式化）",
            example: "2025-11-14 15:30:00",
            required: false
        }
    )
};

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