import { Graph, Cell } from '@antv/x6';

interface ValidationResult {
    valid: boolean;
    message?: string;
}

/**
 * 校验节点配置
 * @param node 节点
 * @param nodeDefinition 节点定义
 */
const validateNodeConfig = (node: Cell, nodeDefinition: any): ValidationResult => {
    const panelVariables = node.getProp('panelVariables');
    const localVariables = node.getProp('localVariables');
    
    // 校验面板变量
    if (nodeDefinition?.panelVariablesSchema?.required) {
        for (const field of nodeDefinition.panelVariablesSchema.required) {
            if (!panelVariables?.[field]) {
                const fieldTitle = nodeDefinition.panelVariablesSchema.properties[field]?.title || field;
                return {
                    valid: false,
                    message: `节点 "${node.attr('label/text')}" 的面板变量 "${fieldTitle}" 是必填项`
                };
            }
        }
    }

    // 校验环境变量
    if (nodeDefinition?.localVariablesSchema?.required) {
        for (const field of nodeDefinition.localVariablesSchema.required) {
            if (!localVariables?.[field]) {
                const fieldTitle = nodeDefinition.localVariablesSchema.properties[field]?.title || field;
                return {
                    valid: false,
                    message: `节点 "${node.attr('label/text')}" 的环境变量 "${fieldTitle}" 是必填项`
                };
            }
        }
    }

    return { valid: true };
};

/**
 * 校验流程图是否为空
 * @param graph 流程图实例
 */
const validateGraphNotEmpty = (graph: Graph): ValidationResult => {
    const nodes = graph.getNodes();
    if (nodes.length === 0) {
        return {
            valid: false,
            message: '流程图中没有任何节点'
        };
    }
    return { valid: true };
};

/**
 * 校验必要节点
 * @param graph 流程图实例
 */
const validateRequiredNodes = (graph: Graph): ValidationResult => {
    const nodes = graph.getNodes();
    
    // 检查开始节点
    const hasStartNode = nodes.some(node => node.getProp('nodeType') === 'START_EVENT');
    if (!hasStartNode) {
        return {
            valid: false,
            message: '流程图中必须包含开始节点'
        };
    }

    // 检查结束节点
    const hasEndNode = nodes.some(node => node.getProp('nodeType') === 'END_EVENT');
    if (!hasEndNode) {
        return {
            valid: false,
            message: '流程图中必须包含结束节点'
        };
    }

    return { valid: true };
};

/**
 * 校验节点连接
 * @param graph 流程图实例
 */
const validateNodeConnections = (graph: Graph): ValidationResult => {
    const nodes = graph.getNodes();
    const edges = graph.getEdges();

    if (edges.length < nodes.length - 1) {
        return {
            valid: false,
            message: '存在未连接的节点，请确保所有节点都已正确连接'
        };
    }

    return { valid: true };
};

/**
 * 校验所有节点配置
 * @param graph 流程图实例
 */
const validateAllNodesConfig = (graph: Graph): ValidationResult => {
    const nodes = graph.getNodes();
    
    for (const node of nodes) {
        const nodeDefinition = node.getProp('nodeDefinition');
        const result = validateNodeConfig(node, nodeDefinition);
        if (!result.valid) {
            return result;
        }
    }

    return { valid: true };
};

/**
 * 校验整个流程图
 * @param graph 流程图实例
 */
export const validateWorkflow = (graph: Graph): ValidationResult => {
    // 按顺序执行所有验证
    const validators = [
        validateGraphNotEmpty,
        validateRequiredNodes,
        validateAllNodesConfig,
        validateNodeConnections
    ];

    for (const validator of validators) {
        const result = validator(graph);
        if (!result.valid) {
            return result;
        }
    }

    return { valid: true };
};
