package tech.easyflow.agent.runtime.artifact;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import tech.easyflow.agent.runtime.AgentRuntimeStateCleanupService;
import tech.easyflow.chatlog.domain.dto.ChatMessageRecord;
import tech.easyflow.chatlog.domain.dto.ChatSessionSummary;
import tech.easyflow.common.web.exceptions.BusinessException;

import java.math.BigInteger;
import java.util.List;

/**
 * {@link AgentArtifactChatSessionExtension} Agent 专属行为测试。
 */
public class AgentArtifactChatSessionExtensionTest {

    /**
     * 验证 Agent 会话删除成功后才按可信归属标记 Artifact。
     */
    @Test
    public void shouldCleanupRuntimeBeforeDeleteAndMarkArtifactAfterDelete() {
        AgentRuntimeStateCleanupService runtimeCleanup = Mockito.mock(AgentRuntimeStateCleanupService.class);
        AgentArtifactService artifactService = Mockito.mock(AgentArtifactService.class);
        AgentArtifactChatSessionExtension extension =
                new AgentArtifactChatSessionExtension(runtimeCleanup, artifactService);
        ChatSessionSummary summary = agentSession();

        extension.beforeDelete(summary, BigInteger.valueOf(2), BigInteger.valueOf(2));
        extension.afterDelete(summary, BigInteger.valueOf(2), BigInteger.valueOf(2));

        Mockito.verify(runtimeCleanup).clearChatSession(BigInteger.valueOf(4), BigInteger.valueOf(2));
        Mockito.verify(artifactService).markSessionDeletePending(
                BigInteger.ONE, BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(4));
    }

    /**
     * 验证非 Agent 会话不匹配扩展。
     */
    @Test
    public void shouldIgnoreNonAgentSession() {
        AgentArtifactChatSessionExtension extension = new AgentArtifactChatSessionExtension(
                Mockito.mock(AgentRuntimeStateCleanupService.class), Mockito.mock(AgentArtifactService.class));
        ChatSessionSummary summary = agentSession();
        summary.setAssistantCode("BOT");

        Assert.assertFalse(extension.supports(summary));
    }

    /**
     * 验证历史投影使用会话中的可信租户、用户、Agent 和会话归属。
     */
    @Test
    public void shouldProjectUsingTrustedSessionIdentity() {
        AgentArtifactService artifactService = Mockito.mock(AgentArtifactService.class);
        AgentArtifactChatSessionExtension extension = new AgentArtifactChatSessionExtension(
                Mockito.mock(AgentRuntimeStateCleanupService.class), artifactService);
        List<ChatMessageRecord> records = List.of(new ChatMessageRecord());

        extension.projectMessages(agentSession(), records);

        Mockito.verify(artifactService).projectHistoryArtifacts(records,
                BigInteger.ONE, BigInteger.valueOf(2), BigInteger.valueOf(3), BigInteger.valueOf(4));
    }

    private ChatSessionSummary agentSession() {
        ChatSessionSummary summary = new ChatSessionSummary();
        summary.setTenantId(BigInteger.ONE);
        summary.setUserId(BigInteger.valueOf(2));
        summary.setAssistantId(BigInteger.valueOf(3));
        summary.setId(BigInteger.valueOf(4));
        summary.setAssistantCode("AGENT");
        return summary;
    }
}
