/*
 * Copyright 2023 - 2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.ai.azure.openai.function;

import java.util.ArrayList;
import java.util.List;

import com.azure.ai.openai.OpenAIClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.core.credential.AzureKeyCredential;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.ai.azure.openai.AzureOpenAiChatClient;
import org.springframework.ai.azure.openai.AzureOpenAiChatOptions;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.function.FunctionCallbackWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(classes = AzureOpenAiChatClientFunctionCallIT.TestConfiguration.class)
@EnabledIfEnvironmentVariable(named = "AZURE_OPENAI_API_KEY", matches = ".+")
@EnabledIfEnvironmentVariable(named = "AZURE_OPENAI_ENDPOINT", matches = ".+")
class AzureOpenAiChatClientFunctionCallIT {

	private static final Logger logger = LoggerFactory.getLogger(AzureOpenAiChatClientFunctionCallIT.class);

	@Autowired
	private AzureOpenAiChatClient chatClient;

	@Test
	void functionCallTest() {

		UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, in Tokyo, and in Paris?");

		List<Message> messages = new ArrayList<>(List.of(userMessage));

		var promptOptions = AzureOpenAiChatOptions.builder()
			.withDeploymentName("gpt-4-0125-preview")
			.withFunctionCallbacks(List.of(FunctionCallbackWrapper.builder(new MockWeatherService())
				.withName("getCurrentWeather")
				.withDescription("Get the current weather in a given location")
				.withResponseConverter((response) -> "" + response.temp() + response.unit())
				.build()))
			.build();

		ChatResponse response = chatClient.call(new Prompt(messages, promptOptions));

		logger.info("Response: {}", response);

		assertThat(response.getResult().getOutput().getContent()).containsAnyOf("30.0", "30");
		assertThat(response.getResult().getOutput().getContent()).containsAnyOf("10.0", "10");
		assertThat(response.getResult().getOutput().getContent()).containsAnyOf("15.0", "15");
	}

	@SpringBootConfiguration
	public static class TestConfiguration {

		@Bean
		public OpenAIClient openAIClient() {
			return new OpenAIClientBuilder().credential(new AzureKeyCredential(System.getenv("AZURE_OPENAI_API_KEY")))
				.endpoint(System.getenv("AZURE_OPENAI_ENDPOINT"))
				.buildClient();
		}

		@Bean
		public AzureOpenAiChatClient azureOpenAiChatClient(OpenAIClient openAIClient) {
			return new AzureOpenAiChatClient(openAIClient,
					AzureOpenAiChatOptions.builder()
						.withDeploymentName("gpt-4-0125-preview")
						.withMaxTokens(500)
						.build());
		}

	}

}

org.springframework.ai.azure.openai.AzureOpenAiChatOptions.builder org.springframework.ai.model.function.FunctionCallbackWrapper.builder
[((2426, 2806), 'org.springframework.ai.azure.openai.AzureOpenAiChatOptions.builder'), ((2426, 2794), 'org.springframework.ai.azure.openai.AzureOpenAiChatOptions.builder'), ((2426, 2503), 'org.springframework.ai.azure.openai.AzureOpenAiChatOptions.builder'), ((2538, 2792), 'org.springframework.ai.model.function.FunctionCallbackWrapper.builder'), ((2538, 2779), 'org.springframework.ai.model.function.FunctionCallbackWrapper.builder'), ((2538, 2698), 'org.springframework.ai.model.function.FunctionCallbackWrapper.builder'), ((2538, 2630), 'org.springframework.ai.model.function.FunctionCallbackWrapper.builder'), ((3652, 3773), 'org.springframework.ai.azure.openai.AzureOpenAiChatOptions.builder'), ((3652, 3758), 'org.springframework.ai.azure.openai.AzureOpenAiChatOptions.builder'), ((3652, 3732), 'org.springframework.ai.azure.openai.AzureOpenAiChatOptions.builder')]