package app.mealsmadeeasy.api.ai;

import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.Nullable;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.content.Media;
import org.springframework.ai.ollama.api.OllamaChatOptions;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class OcrService {

    private final ChatModel chatModel;

    public @Nullable String readMarkdown(Media inputMedia) {
        final Message ocrMessage = UserMessage.builder()
                .text("Convert the text in the given file to Markdown.")
                .media(inputMedia)
                .build();
        final Prompt ocrPrompt = Prompt.builder()
                .messages(ocrMessage)
                .chatOptions(OllamaChatOptions.builder().model("deepseek-ocr:latest").build())
                .build();
        final ChatResponse ocrResponse = this.chatModel.call(ocrPrompt);
        return ocrResponse.getResult().getOutput().getText();
    }

}
