// File generated from our OpenAPI spec by Stainless.

package com.openai.models.chat.completions

import com.openai.core.JsonValue
import com.openai.models.ChatModel
import com.openai.models.FunctionDefinition
import com.openai.models.FunctionParameters
import com.openai.models.ReasoningEffort
import com.openai.models.ResponseFormatText
import kotlin.jvm.optionals.getOrNull
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

internal class ChatCompletionCreateParamsTest {

    @Test
    fun create() {
        ChatCompletionCreateParams.builder()
            .addMessage(
                ChatCompletionDeveloperMessageParam.builder().content("string").name("name").build()
            )
            .model(ChatModel.GPT_4_1)
            .audio(
                ChatCompletionAudioParam.builder()
                    .format(ChatCompletionAudioParam.Format.WAV)
                    .voice(ChatCompletionAudioParam.Voice.ALLOY)
                    .build()
            )
            .frequencyPenalty(-2.0)
            .functionCall(ChatCompletionCreateParams.FunctionCall.FunctionCallMode.NONE)
            .addFunction(
                ChatCompletionCreateParams.Function.builder()
                    .name("name")
                    .description("description")
                    .parameters(
                        FunctionParameters.builder()
                            .putAdditionalProperty("foo", JsonValue.from("bar"))
                            .build()
                    )
                    .build()
            )
            .logitBias(
                ChatCompletionCreateParams.LogitBias.builder()
                    .putAdditionalProperty("foo", JsonValue.from(0))
                    .build()
            )
            .logprobs(true)
            .maxCompletionTokens(0L)
            .maxTokens(0L)
            .metadata(
                ChatCompletionCreateParams.Metadata.builder()
                    .putAdditionalProperty("foo", JsonValue.from("string"))
                    .build()
            )
            .addModality(ChatCompletionCreateParams.Modality.TEXT)
            .n(1L)
            .parallelToolCalls(true)
            .prediction(ChatCompletionPredictionContent.builder().content("string").build())
            .presencePenalty(-2.0)
            .reasoningEffort(ReasoningEffort.LOW)
            .responseFormat(ResponseFormatText.builder().build())
            .seed(-9007199254740991L)
            .serviceTier(ChatCompletionCreateParams.ServiceTier.AUTO)
            .stop("\n")
            .store(true)
            .streamOptions(ChatCompletionStreamOptions.builder().includeUsage(true).build())
            .temperature(1.0)
            .toolChoice(ChatCompletionToolChoiceOption.Auto.NONE)
            .addTool(
                ChatCompletionTool.builder()
                    .function(
                        FunctionDefinition.builder()
                            .name("name")
                            .description("description")
                            .parameters(
                                FunctionParameters.builder()
                                    .putAdditionalProperty("foo", JsonValue.from("bar"))
                                    .build()
                            )
                            .strict(true)
                            .build()
                    )
                    .build()
            )
            .topLogprobs(0L)
            .topP(1.0)
            .user("user-1234")
            .webSearchOptions(
                ChatCompletionCreateParams.WebSearchOptions.builder()
                    .searchContextSize(
                        ChatCompletionCreateParams.WebSearchOptions.SearchContextSize.LOW
                    )
                    .userLocation(
                        ChatCompletionCreateParams.WebSearchOptions.UserLocation.builder()
                            .approximate(
                                ChatCompletionCreateParams.WebSearchOptions.UserLocation.Approximate
                                    .builder()
                                    .city("city")
                                    .country("country")
                                    .region("region")
                                    .timezone("timezone")
                                    .build()
                            )
                            .build()
                    )
                    .build()
            )
            .build()
    }

    @Test
    fun body() {
        val params =
            ChatCompletionCreateParams.builder()
                .addMessage(
                    ChatCompletionDeveloperMessageParam.builder()
                        .content("string")
                        .name("name")
                        .build()
                )
                .model(ChatModel.GPT_4_1)
                .audio(
                    ChatCompletionAudioParam.builder()
                        .format(ChatCompletionAudioParam.Format.WAV)
                        .voice(ChatCompletionAudioParam.Voice.ALLOY)
                        .build()
                )
                .frequencyPenalty(-2.0)
                .functionCall(ChatCompletionCreateParams.FunctionCall.FunctionCallMode.NONE)
                .addFunction(
                    ChatCompletionCreateParams.Function.builder()
                        .name("name")
                        .description("description")
                        .parameters(
                            FunctionParameters.builder()
                                .putAdditionalProperty("foo", JsonValue.from("bar"))
                                .build()
                        )
                        .build()
                )
                .logitBias(
                    ChatCompletionCreateParams.LogitBias.builder()
                        .putAdditionalProperty("foo", JsonValue.from(0))
                        .build()
                )
                .logprobs(true)
                .maxCompletionTokens(0L)
                .maxTokens(0L)
                .metadata(
                    ChatCompletionCreateParams.Metadata.builder()
                        .putAdditionalProperty("foo", JsonValue.from("string"))
                        .build()
                )
                .addModality(ChatCompletionCreateParams.Modality.TEXT)
                .n(1L)
                .parallelToolCalls(true)
                .prediction(ChatCompletionPredictionContent.builder().content("string").build())
                .presencePenalty(-2.0)
                .reasoningEffort(ReasoningEffort.LOW)
                .responseFormat(ResponseFormatText.builder().build())
                .seed(-9007199254740991L)
                .serviceTier(ChatCompletionCreateParams.ServiceTier.AUTO)
                .stop("\n")
                .store(true)
                .streamOptions(ChatCompletionStreamOptions.builder().includeUsage(true).build())
                .temperature(1.0)
                .toolChoice(ChatCompletionToolChoiceOption.Auto.NONE)
                .addTool(
                    ChatCompletionTool.builder()
                        .function(
                            FunctionDefinition.builder()
                                .name("name")
                                .description("description")
                                .parameters(
                                    FunctionParameters.builder()
                                        .putAdditionalProperty("foo", JsonValue.from("bar"))
                                        .build()
                                )
                                .strict(true)
                                .build()
                        )
                        .build()
                )
                .topLogprobs(0L)
                .topP(1.0)
                .user("user-1234")
                .webSearchOptions(
                    ChatCompletionCreateParams.WebSearchOptions.builder()
                        .searchContextSize(
                            ChatCompletionCreateParams.WebSearchOptions.SearchContextSize.LOW
                        )
                        .userLocation(
                            ChatCompletionCreateParams.WebSearchOptions.UserLocation.builder()
                                .approximate(
                                    ChatCompletionCreateParams.WebSearchOptions.UserLocation
                                        .Approximate
                                        .builder()
                                        .city("city")
                                        .country("country")
                                        .region("region")
                                        .timezone("timezone")
                                        .build()
                                )
                                .build()
                        )
                        .build()
                )
                .build()

        val body = params._body()

        assertThat(body.messages())
            .containsExactly(
                ChatCompletionMessageParam.ofDeveloper(
                    ChatCompletionDeveloperMessageParam.builder()
                        .content("string")
                        .name("name")
                        .build()
                )
            )
        assertThat(body.model()).isEqualTo(ChatModel.GPT_4_1)
        assertThat(body.audio())
            .contains(
                ChatCompletionAudioParam.builder()
                    .format(ChatCompletionAudioParam.Format.WAV)
                    .voice(ChatCompletionAudioParam.Voice.ALLOY)
                    .build()
            )
        assertThat(body.frequencyPenalty()).contains(-2.0)
        assertThat(body.functionCall())
            .contains(
                ChatCompletionCreateParams.FunctionCall.ofMode(
                    ChatCompletionCreateParams.FunctionCall.FunctionCallMode.NONE
                )
            )
        assertThat(body.functions().getOrNull())
            .containsExactly(
                ChatCompletionCreateParams.Function.builder()
                    .name("name")
                    .description("description")
                    .parameters(
                        FunctionParameters.builder()
                            .putAdditionalProperty("foo", JsonValue.from("bar"))
                            .build()
                    )
                    .build()
            )
        assertThat(body.logitBias())
            .contains(
                ChatCompletionCreateParams.LogitBias.builder()
                    .putAdditionalProperty("foo", JsonValue.from(0))
                    .build()
            )
        assertThat(body.logprobs()).contains(true)
        assertThat(body.maxCompletionTokens()).contains(0L)
        assertThat(body.maxTokens()).contains(0L)
        assertThat(body.metadata())
            .contains(
                ChatCompletionCreateParams.Metadata.builder()
                    .putAdditionalProperty("foo", JsonValue.from("string"))
                    .build()
            )
        assertThat(body.modalities().getOrNull())
            .containsExactly(ChatCompletionCreateParams.Modality.TEXT)
        assertThat(body.n()).contains(1L)
        assertThat(body.parallelToolCalls()).contains(true)
        assertThat(body.prediction())
            .contains(ChatCompletionPredictionContent.builder().content("string").build())
        assertThat(body.presencePenalty()).contains(-2.0)
        assertThat(body.reasoningEffort()).contains(ReasoningEffort.LOW)
        assertThat(body.responseFormat())
            .contains(
                ChatCompletionCreateParams.ResponseFormat.ofText(
                    ResponseFormatText.builder().build()
                )
            )
        assertThat(body.seed()).contains(-9007199254740991L)
        assertThat(body.serviceTier()).contains(ChatCompletionCreateParams.ServiceTier.AUTO)
        assertThat(body.stop()).contains(ChatCompletionCreateParams.Stop.ofString("\n"))
        assertThat(body.store()).contains(true)
        assertThat(body.streamOptions())
            .contains(ChatCompletionStreamOptions.builder().includeUsage(true).build())
        assertThat(body.temperature()).contains(1.0)
        assertThat(body.toolChoice())
            .contains(
                ChatCompletionToolChoiceOption.ofAuto(ChatCompletionToolChoiceOption.Auto.NONE)
            )
        assertThat(body.tools().getOrNull())
            .containsExactly(
                ChatCompletionTool.builder()
                    .function(
                        FunctionDefinition.builder()
                            .name("name")
                            .description("description")
                            .parameters(
                                FunctionParameters.builder()
                                    .putAdditionalProperty("foo", JsonValue.from("bar"))
                                    .build()
                            )
                            .strict(true)
                            .build()
                    )
                    .build()
            )
        assertThat(body.topLogprobs()).contains(0L)
        assertThat(body.topP()).contains(1.0)
        assertThat(body.user()).contains("user-1234")
        assertThat(body.webSearchOptions())
            .contains(
                ChatCompletionCreateParams.WebSearchOptions.builder()
                    .searchContextSize(
                        ChatCompletionCreateParams.WebSearchOptions.SearchContextSize.LOW
                    )
                    .userLocation(
                        ChatCompletionCreateParams.WebSearchOptions.UserLocation.builder()
                            .approximate(
                                ChatCompletionCreateParams.WebSearchOptions.UserLocation.Approximate
                                    .builder()
                                    .city("city")
                                    .country("country")
                                    .region("region")
                                    .timezone("timezone")
                                    .build()
                            )
                            .build()
                    )
                    .build()
            )
    }

    @Test
    fun bodyWithoutOptionalFields() {
        val params =
            ChatCompletionCreateParams.builder()
                .addDeveloperMessage("string")
                .model(ChatModel.GPT_4_1)
                .build()

        val body = params._body()

        assertThat(body.messages())
            .containsExactly(
                ChatCompletionMessageParam.ofDeveloper(
                    ChatCompletionDeveloperMessageParam.builder().content("string").build()
                )
            )
        assertThat(body.model()).isEqualTo(ChatModel.GPT_4_1)
    }

    @Test
    fun structuredOutputsBuilder() {
        class X(val s: String)

        // Only interested in a few things:
        // - Does the `Builder` type change when `responseFormat(Class<T>)` is called?
        // - Are values already set on the "old" `Builder` preserved in the change-over?
        // - Can new values be set on the "new" `Builder` alongside the "old" values?
        val params =
            ChatCompletionCreateParams.builder()
                .addDeveloperMessage("dev message")
                .model(ChatModel.GPT_4_1)
                .responseFormat(X::class.java) // Creates and return a new builder.
                .addSystemMessage("sys message")
                .build()

        val body = params.rawParams._body()

        assertThat(params).isInstanceOf(StructuredChatCompletionCreateParams::class.java)
        assertThat(params.responseType).isEqualTo(X::class.java)
        assertThat(body.messages())
            .containsExactly(
                ChatCompletionMessageParam.ofDeveloper(
                    ChatCompletionDeveloperMessageParam.builder().content("dev message").build()
                ),
                ChatCompletionMessageParam.ofSystem(
                    ChatCompletionSystemMessageParam.builder().content("sys message").build()
                ),
            )
        assertThat(body.model()).isEqualTo(ChatModel.GPT_4_1)
    }
}
