package ch.phoenixtechnologies.lc4j.workshop;

import io.quarkiverse.langchain4j.runtime.aiservice.GuardrailException;
import io.quarkus.logging.Log;
import io.quarkus.websockets.next.OnOpen;
import io.quarkus.websockets.next.OnTextMessage;
import io.quarkus.websockets.next.WebSocket;

@WebSocket(path = "/customer-support-agent")
public class CustomerSupportAgentWebSocket {
    private final CustomerSupportAgent agent;

    public CustomerSupportAgentWebSocket(CustomerSupportAgent agent) {
        this.agent = agent;
    }

    @OnOpen
    public String onOpen() {
        return "Welcome to Phoenix Technologies AI! How can I help you today";
    }

    @OnTextMessage
    public String onTextMessage(String message) {
        try {
            return agent.chat(message);
        } catch (GuardrailException e) {
            Log.errorf(e, "Error calling the LLM: %s", e.getMessage());
            return "Sorry, I am unable to process your request at the moment. It's not something I'm allowed to do.";
        }
    }
}
