package com.agent.management.engine;

import java.util.List;
import java.util.Map;

/**
 * 工作流执行 SSE 事件
 */
public class WorkflowRunEvent {

    private final String runId;
    private final String type;   // node_status, node_stream, workflow_complete, workflow_error
    private final String nodeId;
    private final String status; // RUNNING, SUCCESS, FAILED, SKIPPED
    private final Object output;
    private final String error;
    private final Boolean success;
    private final List<ExecutionLog> logs;
    /** 节点执行后的完整 variables 快照（debug 视图，仅 node_status SUCCESS 携带） */
    private final Map<String, Object> contextSnapshot;
    // node_stream 事件专用字段
    private final String kind;      // thinking | tool_call | tool_result
    private final String content;   // 增量内容
    private final String toolName;  // 工具名（kind != tool_* 时为 null）

    private WorkflowRunEvent(String runId, String type, String nodeId, String status,
                             Object output, String error, Boolean success, List<ExecutionLog> logs) {
        this(runId, type, nodeId, status, output, error, success, logs, null, null, null, null);
    }

    private WorkflowRunEvent(String runId, String type, String nodeId, String status,
                             Object output, String error, Boolean success, List<ExecutionLog> logs,
                             Map<String, Object> contextSnapshot,
                             String kind, String content, String toolName) {
        this.runId = runId;
        this.type = type;
        this.nodeId = nodeId;
        this.status = status;
        this.output = output;
        this.error = error;
        this.success = success;
        this.logs = logs;
        this.contextSnapshot = contextSnapshot;
        this.kind = kind;
        this.content = content;
        this.toolName = toolName;
    }

    public static WorkflowRunEvent nodeRunning(String runId, String nodeId) {
        return new WorkflowRunEvent(runId, "node_status", nodeId, "RUNNING", null, null, null, null);
    }

    public static WorkflowRunEvent nodeResult(String runId, NodeExecutionResult result) {
        return new WorkflowRunEvent(runId, "node_status", result.getNodeId(),
                result.getStatus().name(), result.getOutput(), result.getError(), null, result.getLogs(),
                result.getContextSnapshot(), null, null, null);
    }

    /**
     * 节点执行过程中的流式增量事件（如 Hermes Agent 的 thinking / 工具调用片段）。
     * 前端应按 nodeId 聚合累加，而非每条独立展示。
     */
    public static WorkflowRunEvent nodeStream(String runId, String nodeId, String kind,
                                              String content, String toolName) {
        return new WorkflowRunEvent(runId, "node_stream", nodeId, null, null, null, null, null,
                null, kind, content, toolName);
    }

    public static WorkflowRunEvent workflowComplete(String runId, Map<String, Object> outputs) {
        return new WorkflowRunEvent(runId, "workflow_complete", null, null, outputs, null, true, null);
    }

    public static WorkflowRunEvent workflowError(String runId, String error) {
        return new WorkflowRunEvent(runId, "workflow_error", null, null, null, error, false, null);
    }

    // Getters (SSE 序列化需要)
    public String getRunId() { return runId; }
    public String getType() { return type; }
    public String getNodeId() { return nodeId; }
    public String getStatus() { return status; }
    public Object getOutput() { return output; }
    public String getError() { return error; }
    public Boolean getSuccess() { return success; }
    public List<ExecutionLog> getLogs() { return logs; }
    public Map<String, Object> getContextSnapshot() { return contextSnapshot; }
    public String getKind() { return kind; }
    public String getContent() { return content; }
    public String getToolName() { return toolName; }
}
