package com.yaoai.ai.providers.config;

import com.yaoai.domain.entity.SystemSetting;
import com.yaoai.domain.mapper.SystemSettingMapper;
import org.junit.jupiter.api.Test;

import java.util.Optional;

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

class PlatformModelConfigServiceTest {

    @Test
    void resolveUsesEnabledPlatformConfigBeforeEnvironmentDefaults() {
        SystemSettingMapper mapper = mock(SystemSettingMapper.class);
        SystemSetting setting = new SystemSetting();
        setting.setSettingKey(PlatformModelConfigService.SETTING_KEY);
        setting.setSettingValue("""
                {
                  "text": {
                    "provider": "agnes",
                    "providerName": "Agnes",
                    "protocol": "openai",
                    "baseUrl": "https://apihub.agnes-ai.com/v1",
                    "apiKey": "agnes-secret",
                    "modelId": "agnes-2.0-flash",
                    "modelName": "Agnes 2.0 Flash",
                    "endpoint": "/chat/completions",
                    "resolveIp": "104.18.18.62",
                    "enabled": true
                  }
                }
                """);
        when(mapper.findByKey(PlatformModelConfigService.SETTING_KEY)).thenReturn(Optional.of(setting));

        PlatformModelConfigService service = new PlatformModelConfigService(mapper, buildArkProperties());

        ResolvedModelConfig config = service.resolve(ModelCapability.TEXT);

        assertEquals("agnes", config.provider());
        assertEquals("openai", config.protocol());
        assertEquals("https://apihub.agnes-ai.com/v1", config.baseUrl());
        assertEquals("agnes-secret", config.apiKey());
        assertEquals("agnes-2.0-flash", config.modelId());
        assertEquals("Agnes 2.0 Flash", config.modelName());
        assertEquals("/chat/completions", config.endpoint());
        assertEquals("104.18.18.62", config.resolveIp());
    }

    @Test
    void resolveFallsBackToVolcenginePropertiesWhenPlatformConfigIsMissing() {
        SystemSettingMapper mapper = mock(SystemSettingMapper.class);
        when(mapper.findByKey(PlatformModelConfigService.SETTING_KEY)).thenReturn(Optional.empty());

        PlatformModelConfigService service = new PlatformModelConfigService(mapper, buildArkProperties());

        ResolvedModelConfig config = service.resolve(ModelCapability.IMAGE);

        assertEquals("volcengine", config.provider());
        assertEquals("seedream", config.protocol());
        assertEquals("https://ark.cn-beijing.volces.com/api/v3", config.baseUrl());
        assertEquals("ark-secret", config.apiKey());
        assertEquals("doubao-seedream-test", config.modelId());
        assertTrue(config.modelName().contains("Seedream"));
        assertEquals("/images/generations", config.endpoint());
    }

    @Test
    void adminSettingsMasksStoredApiKeys() {
        SystemSettingMapper mapper = mock(SystemSettingMapper.class);
        SystemSetting setting = new SystemSetting();
        setting.setSettingKey(PlatformModelConfigService.SETTING_KEY);
        setting.setSettingValue("""
                {
                  "video": {
                    "provider": "agnes",
                    "apiKey": "agnes-secret",
                    "modelId": "agnes-video-v2.0",
                    "enabled": true
                  }
                }
                """);
        when(mapper.findByKey(PlatformModelConfigService.SETTING_KEY)).thenReturn(Optional.of(setting));

        PlatformModelConfigService service = new PlatformModelConfigService(mapper, buildArkProperties());

        PlatformModelSettings settings = service.getAdminSettings();

        assertEquals("******", settings.getVideo().getApiKey());
    }

    private static ArkProperties buildArkProperties() {
        ArkProperties properties = new ArkProperties();
        properties.setApiKey("ark-secret");
        properties.setBaseUrl("https://ark.cn-beijing.volces.com/api/v3");
        properties.setTextModel("doubao-text-test");
        properties.setImageModel("doubao-seedream-test");
        properties.setVideoModel("doubao-seedance-test");
        return properties;
    }
}
