package dev.mokksy.aimocks.openai.official.responses

import com.openai.models.responses.EasyInputMessage
import com.openai.models.responses.ResponseCreateParams
import com.openai.models.responses.ResponseInputContent
import com.openai.models.responses.ResponseInputFile
import com.openai.models.responses.ResponseInputItem
import com.openai.models.responses.ResponseInputText
import dev.mokksy.aimocks.openai.openai
import io.kotest.matchers.string.shouldContainIgnoringCase
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import java.util.UUID
import kotlin.time.measureTimedValue

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class ResponsesFileInputTest : AbstractOpenaiResponsesTest() {
    private val imageResource = this.javaClass.getResource("/pipiro.jpg")!!
    private val filename = imageResource.file
    private val fileId = UUID.randomUUID().toString()

    @BeforeEach
    fun setupMock() {
        openai.responses {
            temperature = temperatureValue
            model = modelName

            containsInputFileWithNamed(filename)
            containsInputFileWithId(fileId)
        } responds {
            assistantContent = "The file is an image."
        }
    }

    @Test
    @Suppress("LongMethod")
    fun `Should match file`() {
        val params =
            createParams(fileId, modelName)

        val timedValue =
            measureTimedValue {
                client.responses().create(params)
            }

        val response = timedValue.value

        logger.debug { "Response: $response" }
        val message = response.output().single().asMessage()

        val assistantText =
            message
                .content()
                .single()
                .asOutputText()
                .text()
        logger.info { "Assistant text: $assistantText" }

        assistantText shouldContainIgnoringCase "image"

        verifyResponse(response)
    }

    private fun createParams(
        id: String,
        name: String,
    ): ResponseCreateParams {
        val input =
            ResponseCreateParams.Input.ofResponse(
                listOf(
                    ResponseInputItem.ofEasyInputMessage(
                        EasyInputMessage
                            .builder()
                            .role(EasyInputMessage.Role.USER)
                            .content(
                                EasyInputMessage.Content
                                    .ofResponseInputMessageContentList(
                                        listOf(
                                            ResponseInputContent.ofInputText(
                                                ResponseInputText
                                                    .builder()
                                                    .text(
                                                        "what's the file?",
                                                    ).build(),
                                            ),
                                            ResponseInputContent.ofInputFile(
                                                ResponseInputFile
                                                    .builder()
                                                    .fileId(id)
                                                    .filename(filename)
                                                    .build(),
                                            ),
                                        ),
                                    ),
                            ).build(),
                    ),
                ),
            )
        val params =
            ResponseCreateParams
                .builder()
                .temperature(temperatureValue)
                .maxOutputTokens(maxCompletionTokensValue)
                .model(name)
                .instructions("You are a file expert")
                .input(input)
                .build()
        return params
    }
}
