package com.qqchen.deploy.backend.workflow.delegate;

import com.qqchen.deploy.backend.workflow.dto.inputmapping.ShellInputMapping;
import com.qqchen.deploy.backend.workflow.dto.outputs.ShellOutputs;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Shell脚本节点委派
 *
 * @author qqchen
 * @since 2025-10-22
 */
@Slf4j
@Component("shellDelegate")
public class ShellNodeDelegate extends BaseNodeDelegate<ShellInputMapping, ShellOutputs> {

    @Override
    protected void executeInternal(
            DelegateExecution execution,
            Map<String, Object> configs,
            ShellInputMapping input
    ) {
        if (input.getScript() == null || input.getScript().isEmpty()) {
            // ❌ 致命错误：脚本为空
            terminateWorkflow("Script is required but not provided");
        }
        
        log.info("Executing shell script: {}", input.getScript());
        
        // TODO: 实现Shell脚本执行逻辑
        // 目前先返回模拟结果
        // ✅ 直接修改预初始化的 output 对象
        output.setExitCode(0);
        output.setStdout("Shell execution completed (mocked)");
        output.setStderr("");
        
        // ✅ 不需要 return，status 已经是 SUCCESS
    }
}
