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

import com.qqchen.deploy.backend.workflow.constants.WorkFlowConstants;
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.BpmnError;
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 ShellOutputs executeInternal(
            DelegateExecution execution,
            Map<String, Object> configs,
            ShellInputMapping input
    ) {
        if (input.getScript() == null || input.getScript().isEmpty()) {
            throw new BpmnError(WorkFlowConstants.WORKFLOW_EXEC_ERROR, 
                "Script is required but not provided");
        }
        
        log.info("Executing shell script: {}", input.getScript());
        
        // TODO: 实现Shell脚本执行逻辑
        // 目前先返回模拟结果
        ShellOutputs outputs = new ShellOutputs();
        outputs.setExitCode(0);
        outputs.setStdout("Shell execution completed (mocked)");
        outputs.setStderr("");
        
        return outputs;
    }
}
