package com.infoepoch.pms.agent.platform.chatbot.application;

import com.infoepoch.pms.agent.platform.agent.application.AgentQueryService;
import com.infoepoch.pms.agent.platform.chatbot.application.dto.CreateChatbotRequest;
import com.infoepoch.pms.agent.platform.chatbot.domain.irepository.IChatbotAgentsRepository;
import com.infoepoch.pms.agent.platform.chatbot.domain.irepository.IChatbotRepository;
import com.infoepoch.pms.agent.platform.model.application.ModelQueryService;
import com.infoepoch.pms.agent.platform.model.domain.AiModelConfig;
import com.infoepoch.pms.agent.platform.model.domain.AiModelProvider;
import com.infoepoch.pms.agent.platform.shared.exception.BusinessException;
import com.infoepoch.pms.agent.platform.shared.response.ResultCode;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.math.BigDecimal;
import java.time.LocalDateTime;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class ChatbotServiceTest {

    @Mock
    private IChatbotRepository chatbotRepository;

    @Mock
    private IChatbotAgentsRepository chatbotAgentsRepository;

    @Mock
    private ModelQueryService modelQueryService;

    @Mock
    private AgentQueryService agentQueryService;

    @Mock
    private ChatbotQueryService chatbotQueryService;

    @Mock
    private ChatbotToolQueryService chatbotToolQueryService;

    @Test
    void shouldRejectCreatingChatbotWithRerankModelConfig() {
        CreateChatbotRequest req = new CreateChatbotRequest();
        req.setName("客服机器人");
        req.setDescription("回答问题");
        req.setModelConfigId("config-rerank");

        when(chatbotRepository.existsByTrimmedName("客服机器人")).thenReturn(false);
        when(modelQueryService.getModelConfigOrNull("config-rerank")).thenReturn(rerankConfig());

        ChatbotService service = new ChatbotService(
                chatbotRepository,
                chatbotAgentsRepository,
                modelQueryService,
                agentQueryService,
                chatbotQueryService,
                chatbotToolQueryService
        );

        BusinessException ex = assertThrows(BusinessException.class, () -> service.create(req));

        assertEquals(ResultCode.PARAM_ERROR, ex.getResultCode());
    }

    private AiModelConfig rerankConfig() {
        AiModelProvider provider = AiModelProvider.restore(
                "provider-1",
                "DashScope",
                "DASHSCOPE",
                null,
                "sk-test",
                null,
                "provider",
                true,
                null,
                null,
                LocalDateTime.now(),
                LocalDateTime.now()
        );
        return new AiModelConfig(
                provider,
                "gte-rerank",
                "RERANK",
                "Rerank",
                1024,
                BigDecimal.ONE,
                8,
                256,
                Boolean.TRUE,
                Boolean.TRUE,
                null
        );
    }
}
