package com.nashss.se.yodaservice.activity;

import com.nashss.se.yodaservice.activity.requests.AiRequest;
import com.nashss.se.yodaservice.activity.results.AiResult;
import com.nashss.se.yodaservice.dynamodb.PHRDAO;
import com.nashss.se.yodaservice.dynamodb.models.PHR;
import com.nashss.se.yodaservice.prompt.PromptStandard;
import com.theokanning.openai.completion.chat.ChatCompletionRequest;
import com.theokanning.openai.completion.chat.ChatMessage;
import com.theokanning.openai.completion.chat.ChatMessageRole;
import com.theokanning.openai.service.OpenAiService;

import javax.inject.Inject;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AiActivity {

    private final OpenAiService openAiService;
    private final PHRDAO phrdao;

    @Inject
    public AiActivity(OpenAiService openAiService1, PHRDAO phrdao) {
        this.openAiService = openAiService1;
        this.phrdao = phrdao;
    }

    public AiResult handleRequest(final AiRequest request) {
        try {
            PHR existingPhr = phrdao.getPHR(request.getPhrId(), request.getDate());
            PromptStandard prompt = new PromptStandard(existingPhr.getComprehendData(), existingPhr.getTranscription());
            List<ChatMessage> messages = new ArrayList<>();
            ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(),prompt.toString());
            messages.add(userMessage);
            ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                    .model("gpt-4")
                    .messages(messages)
                    .logitBias(new HashMap<>())
                    .build();
            try {
                System.out.println("Completion initiated");
                String completion = openAiService.createChatCompletion(chatCompletionRequest).getChoices().get(0).getMessage().getContent();
                try {
                    List<String> pastAi = existingPhr.getAiDifferentials();
                    pastAi.add(completion);
                    existingPhr.setAiDifferentials(pastAi);
                    phrdao.savePHR(existingPhr);
                } catch (NullPointerException npe) {
                    List<String> pastAi = new ArrayList<>();
                    pastAi.add(completion);
                    existingPhr.setAiDifferentials(pastAi);
                    phrdao.savePHR(existingPhr);
                }
                return AiResult.builder()
                        .withDifferential(completion)
                        .build();
            } catch (RuntimeException RE) {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                RE.printStackTrace(pw);
                throw new RuntimeException("Error while creating chat completion with OpenAI service: \n" + sw);
            }
        } catch (Exception RE) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            RE.printStackTrace(pw);
            throw new RuntimeException("Error while receiving completion data: \n" + sw);
        }
    }

}


com.theokanning.openai.completion.chat.ChatMessageRole.USER.value com.theokanning.openai.completion.chat.ChatCompletionRequest.builder
[((1400, 1428), 'com.theokanning.openai.completion.chat.ChatMessageRole.USER.value'), ((1546, 1730), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1546, 1701), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1546, 1653), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((1546, 1613), 'com.theokanning.openai.completion.chat.ChatCompletionRequest.builder'), ((2510, 2615), 'com.nashss.se.yodaservice.activity.results.AiResult.builder'), ((2510, 2582), 'com.nashss.se.yodaservice.activity.results.AiResult.builder')]