package com.agent.management.engine;

import java.time.Instant;

/**
 * 节点执行过程中的一条日志记录
 */
public class ExecutionLog {

    /**
     * 节点执行日志类型。
     *
     * <p>v1.1 扩展：新增 STATUS / STEP / TOOL_PROGRESS / TODO_UPDATE / CLARIFY_REQUEST，
     * 用于 Hermes 思考过程展示重构的 5 区域。前端按 type 分类渲染。</p>
     */
    public enum Type {
        INFO,
        TOOL_CALL,
        TOOL_RESULT,
        THINKING,
        ERROR,
        /** 实时状态文案（覆盖式，如"压缩上下文中"、"等待用户回答"） */
        STATUS,
        /** 每轮 LLM 调用前的进度（iteration / prev_tools） */
        STEP,
        /** 工具细粒度进度（tool.started / reasoning.available） */
        TOOL_PROGRESS,
        /** todo 工具的完整 JSON 输出，前端解析后渲染 TODO List */
        TODO_UPDATE,
        /** 模型请求用户确认（含 clarify_id / question / choices） */
        CLARIFY_REQUEST,
    }

    private final Type type;
    private final Instant timestamp;
    private final String message;
    private final String detail;  // 可选的额外信息（如工具参数、返回摘要）

    public ExecutionLog(Type type, String message, String detail) {
        this.type = type;
        this.timestamp = Instant.now();
        this.message = message;
        this.detail = detail;
    }

    public static ExecutionLog info(String message) {
        return new ExecutionLog(Type.INFO, message, null);
    }

    public static ExecutionLog toolCall(String toolName, String argsSummary) {
        return new ExecutionLog(Type.TOOL_CALL, toolName, argsSummary);
    }

    public static ExecutionLog toolResult(String toolName, String resultSummary) {
        return new ExecutionLog(Type.TOOL_RESULT, toolName, resultSummary);
    }

    public static ExecutionLog thinking(String text) {
        return new ExecutionLog(Type.THINKING, text, null);
    }

    public static ExecutionLog error(String message) {
        return new ExecutionLog(Type.ERROR, message, null);
    }

    /** v1.1：实时状态文案（覆盖式） */
    public static ExecutionLog status(String message) {
        return new ExecutionLog(Type.STATUS, message, null);
    }

    /** v1.1：每轮 LLM 调用前的进度（message=iteration序号，detail=prev_tools JSON） */
    public static ExecutionLog step(int iteration, String prevToolsJson) {
        return new ExecutionLog(Type.STEP, "iteration " + iteration, prevToolsJson);
    }

    /** v1.1：工具细粒度进度（message=event_type，detail=preview/args JSON） */
    public static ExecutionLog toolProgress(String eventType, String detailJson) {
        return new ExecutionLog(Type.TOOL_PROGRESS, eventType, detailJson);
    }

    /** v1.1：todo 工具完整输出（detail=完整 JSON） */
    public static ExecutionLog todoUpdate(String fullJson) {
        return new ExecutionLog(Type.TODO_UPDATE, "todo", fullJson);
    }

    /** v1.1：模型请求用户确认（message=question，detail=choices JSON） */
    public static ExecutionLog clarifyRequest(String clarifyId, String question, String choicesJson) {
        return new ExecutionLog(Type.CLARIFY_REQUEST, question, "{\"clarify_id\":\"" + clarifyId + "\",\"choices\":" + choicesJson + "}");
    }

    public Type getType() { return type; }
    public Instant getTimestamp() { return timestamp; }
    public String getMessage() { return message; }
    public String getDetail() { return detail; }
}
