/**
 * Copyright (c) 2025-2026, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.easyagents.flow.core.chain.event;


import com.easyagents.flow.core.chain.Chain;
import com.easyagents.flow.core.chain.Node;
import com.easyagents.flow.core.chain.NodeStatus;

/**
 * 节点开始执行事件。
 */
public class NodeStartEvent extends BaseEvent {

    private final Node node;
    private final String executionAttemptKey;
    private final NodeStatus status;
    private final String auditInstanceId;

    /**
     * 创建节点开始事件。
     *
     * @param chain 当前工作流
     * @param node 当前节点
     */
    public NodeStartEvent(Chain chain, Node node) {
        this(chain, node, null, null,
                chain.getStateInstanceId());
    }

    /**
     * 创建携带不可变业务尝试键的节点开始事件。
     *
     * @param chain 当前工作流
     * @param node 当前节点
     * @param executionAttemptKey 节点本次业务尝试键
     */
    public NodeStartEvent(Chain chain,
                          Node node,
                          String executionAttemptKey) {
        this(chain, node, executionAttemptKey, null,
                chain.getStateInstanceId());
    }

    /**
     * 创建携带不可变业务尝试键和节点状态的开始事件。
     *
     * @param chain 当前工作流
     * @param node 当前节点
     * @param executionAttemptKey 节点本次业务尝试键
     * @param status 事件创建时的节点状态
     */
    public NodeStartEvent(Chain chain,
                          Node node,
                          String executionAttemptKey,
                          NodeStatus status) {
        this(chain, node, executionAttemptKey, status,
                chain.getStateInstanceId());
    }

    /**
     * 创建携带完整不可变审计上下文的节点开始事件。
     *
     * @param chain 当前工作流
     * @param node 当前节点
     * @param executionAttemptKey 节点本次业务尝试键
     * @param status 事件创建时的节点状态
     * @param auditInstanceId 节点审计应关联的顶级执行实例 ID
     */
    public NodeStartEvent(Chain chain,
                          Node node,
                          String executionAttemptKey,
                          NodeStatus status,
                          String auditInstanceId) {
        super(chain);
        this.node = node;
        this.executionAttemptKey = executionAttemptKey;
        this.status = status;
        this.auditInstanceId = auditInstanceId;
    }

    /**
     * 获取当前节点。
     *
     * @return 当前节点
     */
    public Node getNode() {
        return node;
    }

    /**
     * 获取事件创建时捕获的业务尝试键。
     *
     * @return 业务尝试键；旧调用方未提供时为 {@code null}
     */
    public String getExecutionAttemptKey() {
        return executionAttemptKey;
    }

    /**
     * 获取事件创建时捕获的节点状态。
     *
     * @return 节点状态；旧调用方未提供时为 {@code null}
     */
    public NodeStatus getStatus() {
        return status;
    }

    /**
     * 获取节点审计关联的顶级执行实例 ID。
     *
     * @return 顶级执行实例 ID
     */
    public String getAuditInstanceId() {
        return auditInstanceId;
    }

    @Override
    public String toString() {
        return "NodeStartEvent{" +
            "node=" + node +
            ", executionAttemptKey='" + executionAttemptKey + '\'' +
            ", status=" + status +
            ", auditInstanceId='" + auditInstanceId + '\'' +
            ", chain=" + chain +
            '}';
    }
}
