package tech.easyflow.agent.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import tech.easyflow.agent.entity.Agent;
import tech.easyflow.agent.entity.AgentSkillBinding;
import tech.easyflow.agent.service.AgentService;
import tech.easyflow.agent.service.AgentSkillBindingService;

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

/**
 * Agent 对 Skill 的生命周期引用查询测试。
 */
public class AgentSkillReferenceProviderTest {

    /**
     * 草稿绑定和已发布冻结绑定都应阻止 Skill 下线或删除，并按 Agent 去重。
     */
    @Test
    public void shouldIncludeDraftAndPublishedSkillReferences() {
        AgentService agentService = Mockito.mock(AgentService.class);
        AgentSkillBindingService bindingService = Mockito.mock(AgentSkillBindingService.class);
        AgentSkillBinding draftBinding = new AgentSkillBinding();
        draftBinding.setAgentId(BigInteger.ONE);
        draftBinding.setSkillId(BigInteger.TEN);
        Agent published = new Agent();
        published.setId(BigInteger.TWO);
        published.setPublishedSnapshotJson(Map.of(
                "skillBindings", List.of(Map.of("skillId", BigInteger.TEN))));
        Agent draftAgent = agent(BigInteger.ONE, "草稿引用智能体");
        Agent publishedAgent = agent(BigInteger.TWO, "线上引用智能体");
        Mockito.when(bindingService.list(Mockito.any(QueryWrapper.class)))
                .thenReturn(List.of(draftBinding));
        Mockito.when(agentService.list(Mockito.any(QueryWrapper.class)))
                .thenReturn(List.of(published));
        Mockito.when(agentService.listByIds(Mockito.anyCollection()))
                .thenReturn(List.of(draftAgent, publishedAgent));
        AgentSkillReferenceProvider provider = new AgentSkillReferenceProvider(
                agentService, bindingService);

        List<String> references = provider.listReferences(BigInteger.TEN);

        Assert.assertEquals(List.of(
                "智能体“草稿引用智能体”",
                "智能体“线上引用智能体”"), references);
    }

    /**
     * 创建 Agent 摘要。
     *
     * @param id Agent ID
     * @param name 名称
     * @return Agent
     */
    private Agent agent(BigInteger id, String name) {
        Agent agent = new Agent();
        agent.setId(id);
        agent.setName(name);
        return agent;
    }
}
