package com.infoepoch.pms.agent.platform.model.domain;

import com.infoepoch.pms.agent.platform.shared.exception.BusinessException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class AiModelConfigTest {

    private AiModelProvider provider;

    @BeforeEach
    void setUp() {
        provider = new AiModelProvider(
                "DashScope",
                "DASHSCOPE",
                "https://dashscope.aliyuncs.com/compatible-mode/v1",
                "sk-test",
                null,
                "provider"
        );
    }

    @Test
    void shouldKeepLlmOnlyFieldsForLlmModel() {
        AiModelConfig config = new AiModelConfig(
                provider,
                "qwen-max",
                "LLM",
                "Qwen Max",
                2048,
                BigDecimal.valueOf(0.7),
                20,
                1536,
                Boolean.TRUE,
                Boolean.FALSE,
                Map.of("mode", "chat")
        );

        assertEquals("LLM", config.getModelType());
        assertEquals(2048, config.getMaxTokens());
        assertEquals(BigDecimal.valueOf(0.7), config.getTemperature());
        assertEquals(20, config.getContextWindow());
        assertNull(config.getVectorDimension());
        assertTrue(config.getEnableThinking());
        assertFalse(config.getMultiModel());
    }

    @Test
    void shouldNullLlmOnlyFieldsForEmbeddingModel() {
        AiModelConfig config = new AiModelConfig(
                provider,
                "text-embedding-v3",
                "EMBEDDING",
                "Embedding",
                2048,
                BigDecimal.valueOf(0.7),
                20,
                1024,
                Boolean.TRUE,
                Boolean.TRUE,
                null
        );

        assertEquals("EMBEDDING", config.getModelType());
        assertNull(config.getMaxTokens());
        assertNull(config.getTemperature());
        assertNull(config.getContextWindow());
        assertEquals(1024, config.getVectorDimension());
        assertNull(config.getEnableThinking());
        assertNull(config.getMultiModel());
    }

    @Test
    void shouldNullTypeSpecificFieldsForRerankModel() {
        AiModelConfig config = new AiModelConfig(
                provider,
                "gte-rerank",
                "RERANK",
                "Rerank",
                2048,
                BigDecimal.valueOf(0.7),
                20,
                1024,
                Boolean.TRUE,
                Boolean.TRUE,
                null
        );

        assertEquals("RERANK", config.getModelType());
        assertNull(config.getMaxTokens());
        assertNull(config.getTemperature());
        assertNull(config.getContextWindow());
        assertNull(config.getVectorDimension());
        assertNull(config.getEnableThinking());
        assertNull(config.getMultiModel());
    }

    @Test
    void shouldClearStaleFieldsWhenSwitchingFromEmbeddingToLlm() {
        AiModelConfig config = new AiModelConfig(
                provider,
                "text-embedding-v3",
                "EMBEDDING",
                "Embedding",
                null,
                null,
                null,
                1536,
                null,
                null,
                null
        );

        config.modify(
                "qwen-max",
                "LLM",
                "Qwen Max",
                4096,
                BigDecimal.valueOf(0.2),
                16,
                1536,
                Boolean.FALSE,
                Boolean.TRUE,
                Map.of("mode", "chat")
        );

        assertEquals("LLM", config.getModelType());
        assertEquals(4096, config.getMaxTokens());
        assertEquals(BigDecimal.valueOf(0.2), config.getTemperature());
        assertEquals(16, config.getContextWindow());
        assertNull(config.getVectorDimension());
        assertFalse(config.getEnableThinking());
        assertTrue(config.getMultiModel());
    }

    @Test
    void shouldRejectUnknownModelType() {
        assertThrows(BusinessException.class, () -> new AiModelConfig(
                provider,
                "model-x",
                "IMAGE",
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null
        ));
    }
}
