package kz.konturai.parser.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;

class OpenAIAnalyticsServiceRequestBodyTest {

    @Test
    void buildChatMessages_shouldPrependSystemMessage_whenProvided() {
        OpenAIAnalyticsService svc = new OpenAIAnalyticsService("http://localhost");

        List<Map<String, Object>> messages = svc.buildChatMessages("USER", "SYSTEM");

        assertEquals(2, messages.size());
        assertEquals("system", messages.get(0).get("role"));
        assertEquals("SYSTEM", messages.get(0).get("content"));
        assertEquals("user", messages.get(1).get("role"));
        assertEquals("USER", messages.get(1).get("content"));
    }

    @Test
    void buildChatMessages_shouldNotAddSystemMessage_whenBlank() {
        OpenAIAnalyticsService svc = new OpenAIAnalyticsService("http://localhost");

        List<Map<String, Object>> messages = svc.buildChatMessages("USER", "   ");

        assertEquals(1, messages.size());
        assertEquals("user", messages.get(0).get("role"));
        assertEquals("USER", messages.get(0).get("content"));
    }

    @Test
    void buildChatCompletionsRequestBody_shouldReflectMaxTokensOverrideAndMessages() {
        OpenAIAnalyticsService svc = new OpenAIAnalyticsService("http://localhost");

        List<Map<String, Object>> messages = List.of(
                Map.of("role", "system", "content", "SYS"),
                Map.of("role", "user", "content", "USR"));

        Map<String, Object> body = svc.buildChatCompletionsRequestBody("gpt-test", messages, 24000, 0.3);

        assertEquals("gpt-test", body.get("model"));
        assertEquals(24000, body.get("max_tokens"));
        assertNotNull(body.get("messages"));
        assertEquals(messages, body.get("messages"));
        assertEquals(0.3, ((Number) body.get("temperature")).doubleValue(), 1e-9);
    }
}


