package com.sino.mci.inbound.engine;

import com.sino.mci.inbound.entity.MessageEvent;
import com.sino.mci.inbound.intent.IntentResult;
import lombok.Data;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 流程执行上下文。
 *
 * <p>包含当前消息事件、业务上下文、意图识别结果以及流程变量，
 * 供条件表达式（SpEL）与节点执行使用。</p>
 */
@Data
public class FlowContext {

    private final MessageEvent event;
    private final Map<String, Object> businessContext;
    private IntentResult intent;
    private final Map<String, Object> variables = new HashMap<>();

    public FlowContext(MessageEvent event, Map<String, Object> businessContext) {
        this.event = event;
        this.businessContext = businessContext != null ? businessContext : new LinkedHashMap<>();
    }

    /**
     * 构建用于 SpEL 表达式求值的根对象。
     */
    public Map<String, Object> toEvaluationMap() {
        Map<String, Object> root = new HashMap<>();
        root.put("event", event);
        root.put("businessContext", businessContext);
        root.put("intent", toIntentMap());
        root.put("variables", variables);
        return root;
    }

    private Map<String, Object> toIntentMap() {
        if (intent == null) {
            return null;
        }
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("intent", intent.getIntent());
        map.put("confidence", intent.getConfidence());
        map.put("slots", intent.getSlots());
        map.put("missingSlots", intent.getMissingSlots());
        map.put("requiresConfirmation", intent.isRequiresConfirmation());
        return map;
    }
}
