/*
 * 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.openai.audio.api;

import java.io.File;
import java.io.IOException;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;

import org.springframework.ai.openai.api.OpenAiAudioApi;
import org.springframework.ai.openai.api.OpenAiAudioApi.SpeechRequest;
import org.springframework.ai.openai.api.OpenAiAudioApi.TranscriptionRequest;
import org.springframework.ai.openai.api.OpenAiAudioApi.StructuredResponse;
import org.springframework.ai.openai.api.OpenAiAudioApi.TranslationRequest;
import org.springframework.ai.openai.api.OpenAiAudioApi.SpeechRequest.Voice;
import org.springframework.ai.openai.api.OpenAiAudioApi.TtsModel;
import org.springframework.ai.openai.api.OpenAiAudioApi.WhisperModel;
import org.springframework.util.FileCopyUtils;

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

/**
 * @author Christian Tzolov
 */
@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+")
public class OpenAiAudioApiIT {

	OpenAiAudioApi audioApi = new OpenAiAudioApi(System.getenv("OPENAI_API_KEY"));

	@SuppressWarnings("null")
	@Test
	void speechTranscriptionAndTranslation() throws IOException {

		byte[] speech = audioApi
			.createSpeech(SpeechRequest.builder()
				.withModel(TtsModel.TTS_1_HD.getValue())
				.withInput("Hello, my name is Chris and I love Spring A.I.")
				.withVoice(Voice.ONYX)
				.build())
			.getBody();

		assertThat(speech).isNotEmpty();

		FileCopyUtils.copy(speech, new File("target/speech.mp3"));

		StructuredResponse translation = audioApi
			.createTranslation(
					TranslationRequest.builder().withModel(WhisperModel.WHISPER_1.getValue()).withFile(speech).build(),
					StructuredResponse.class)
			.getBody();

		assertThat(translation.text().replaceAll(",", "")).isEqualTo("Hello my name is Chris and I love Spring AI.");

		StructuredResponse transcriptionEnglish = audioApi.createTranscription(
				TranscriptionRequest.builder().withModel(WhisperModel.WHISPER_1.getValue()).withFile(speech).build(),
				StructuredResponse.class)
			.getBody();

		assertThat(transcriptionEnglish.text().replaceAll(",", ""))
			.isEqualTo("Hello my name is Chris and I love Spring AI.");

		StructuredResponse transcriptionDutch = audioApi
			.createTranscription(TranscriptionRequest.builder().withFile(speech).withLanguage("nl").build(),
					StructuredResponse.class)
			.getBody();

		assertThat(transcriptionDutch.text()).isEqualTo("Hallo, mijn naam is Chris en ik hou van Spring AI.");
	}

}

org.springframework.ai.openai.api.OpenAiAudioApi.WhisperModel.WHISPER_1.getValue org.springframework.ai.openai.api.OpenAiAudioApi.TranslationRequest.builder org.springframework.ai.openai.api.OpenAiAudioApi.TtsModel.TTS_1_HD.getValue org.springframework.ai.openai.api.OpenAiAudioApi.TranscriptionRequest.builder org.springframework.ai.openai.api.OpenAiAudioApi.SpeechRequest.builder
[((1866, 2039), 'org.springframework.ai.openai.api.OpenAiAudioApi.SpeechRequest.builder'), ((1866, 2026), 'org.springframework.ai.openai.api.OpenAiAudioApi.SpeechRequest.builder'), ((1866, 1999), 'org.springframework.ai.openai.api.OpenAiAudioApi.SpeechRequest.builder'), ((1866, 1934), 'org.springframework.ai.openai.api.OpenAiAudioApi.SpeechRequest.builder'), ((1905, 1933), 'org.springframework.ai.openai.api.OpenAiAudioApi.TtsModel.TTS_1_HD.getValue'), ((2227, 2325), 'org.springframework.ai.openai.api.OpenAiAudioApi.TranslationRequest.builder'), ((2227, 2317), 'org.springframework.ai.openai.api.OpenAiAudioApi.TranslationRequest.builder'), ((2227, 2300), 'org.springframework.ai.openai.api.OpenAiAudioApi.TranslationRequest.builder'), ((2266, 2299), 'org.springframework.ai.openai.api.OpenAiAudioApi.WhisperModel.WHISPER_1.getValue'), ((2565, 2665), 'org.springframework.ai.openai.api.OpenAiAudioApi.TranscriptionRequest.builder'), ((2565, 2657), 'org.springframework.ai.openai.api.OpenAiAudioApi.TranscriptionRequest.builder'), ((2565, 2640), 'org.springframework.ai.openai.api.OpenAiAudioApi.TranscriptionRequest.builder'), ((2606, 2639), 'org.springframework.ai.openai.api.OpenAiAudioApi.WhisperModel.WHISPER_1.getValue'), ((2914, 2988), 'org.springframework.ai.openai.api.OpenAiAudioApi.TranscriptionRequest.builder'), ((2914, 2980), 'org.springframework.ai.openai.api.OpenAiAudioApi.TranscriptionRequest.builder'), ((2914, 2961), 'org.springframework.ai.openai.api.OpenAiAudioApi.TranscriptionRequest.builder')]