/**
 * 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;

import java.util.Map;

/**
 * 节点结束执行事件。
 */
public class NodeEndEvent extends BaseEvent {

    private final Node node;
    private final Map<String, Object> result;
    private final Throwable error;
    private final NodeStatus status;
    private final String executionAttemptKey;

    /**
     * 创建节点结束事件。
     *
     * @param chain 当前工作流
     * @param node 当前节点
     * @param result 节点输出
     * @param error 节点异常
     */
    public NodeEndEvent(Chain chain, Node node, Map<String, Object> result, Throwable error) {
        this(chain, node, result, error, null, null);
    }

    /**
     * 创建携带不可变业务尝试键的节点结束事件。
     *
     * @param chain 当前工作流
     * @param node 当前节点
     * @param result 节点输出
     * @param error 节点异常
     * @param executionAttemptKey 节点本次业务尝试键
     */
    public NodeEndEvent(Chain chain,
                        Node node,
                        Map<String, Object> result,
                        Throwable error,
                        String executionAttemptKey) {
        this(
                chain,
                node,
                result,
                error,
                null,
                executionAttemptKey);
    }

    /**
     * 创建携带不可变节点终态和业务尝试键的节点结束事件。
     *
     * @param chain 当前工作流
     * @param node 当前节点
     * @param result 节点输出
     * @param error 节点异常
     * @param status 节点本次业务尝试终态
     * @param executionAttemptKey 节点本次业务尝试键
     */
    public NodeEndEvent(Chain chain,
                        Node node,
                        Map<String, Object> result,
                        Throwable error,
                        NodeStatus status,
                        String executionAttemptKey) {
        super(chain);
        this.node = node;
        this.result = result;
        this.error = error;
        this.status = status;
        this.executionAttemptKey = executionAttemptKey;
    }

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

    /**
     * 获取节点输出。
     *
     * @return 节点输出
     */
    public Map<String, Object> getResult() {
        return result;
    }

    /**
     * 获取节点异常。
     *
     * @return 节点异常；成功时为 {@code null}
     */
    public Throwable getError() {
        return error;
    }

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

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

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