package com.infoepoch.pms.agent.domain.care.log.hook;

import com.alibaba.cloud.ai.graph.OverAllState;
import com.alibaba.cloud.ai.graph.RunnableConfig;
import com.alibaba.cloud.ai.graph.agent.hook.AgentHook;
import com.alibaba.cloud.ai.graph.agent.hook.HookPosition;
import com.infoepoch.pms.agent.common.utils.LogHelper;
import com.infoepoch.pms.agent.domain.care.log.CareTraceLogSupport;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * care agent 生命周期日志 hook。
 */
public class CareAgentLifecycleLogHook extends AgentHook {

    private static final String HOOK_NAME = "careAgentLifecycleLogHook";

    @Override
    public String getName() {
        return HOOK_NAME;
    }

    @Override
    public int getOrder() {
        return 0;
    }

    @Override
    public HookPosition[] getHookPositions() {
        return new HookPosition[]{HookPosition.BEFORE_AGENT, HookPosition.AFTER_AGENT};
    }

    @Override
    public CompletableFuture<Map<String, Object>> beforeAgent(OverAllState state, RunnableConfig runnableConfig) {
        LogHelper.info(this, CareTraceLogSupport.format(
                CareTraceLogSupport.resolveTraceId(runnableConfig),
                CareTraceLogSupport.resolveSessionId(runnableConfig),
                "Agent开始",
                "场景=%s，agent=%s".formatted(
                        CareTraceLogSupport.resolveScene(runnableConfig),
                        getAgentName()
                )
        ));
        return CompletableFuture.completedFuture(Collections.emptyMap());
    }

    @Override
    public CompletableFuture<Map<String, Object>> afterAgent(OverAllState state, RunnableConfig runnableConfig) {
        LogHelper.info(this, CareTraceLogSupport.format(
                CareTraceLogSupport.resolveTraceId(runnableConfig),
                CareTraceLogSupport.resolveSessionId(runnableConfig),
                "Agent完成",
                "场景=%s，agent=%s".formatted(
                        CareTraceLogSupport.resolveScene(runnableConfig),
                        getAgentName()
                )
        ));
        return CompletableFuture.completedFuture(Collections.emptyMap());
    }
}
