package ch.phoenixtechnologies.lc4j.workshop;

import ch.phoenixtechnologies.lc4j.workshop.guardrails.PromptInjectionGuard;
import ch.phoenixtechnologies.lc4j.workshop.persistence.BookingRepository;
import dev.langchain4j.service.SystemMessage;
import io.quarkiverse.langchain4j.RegisterAiService;
import io.quarkiverse.langchain4j.ToolBox;
import io.quarkiverse.langchain4j.guardrails.InputGuardrails;
import jakarta.enterprise.context.SessionScoped;
import org.eclipse.microprofile.faulttolerance.*;

@SessionScoped
@RegisterAiService
public interface CustomerSupportAgent {
    @SystemMessage(
            """
            You are a customer support agent of a IT company 'Phoenix Technologies AG based in Switzerland.
            You are friendly, polite and concise.
            If the question is unrelated to IT services, you should politely redirect the customer to the right department.

            Today is {current_date}
            """)
    @InputGuardrails(PromptInjectionGuard.class)
    @ToolBox(BookingRepository.class)
    @Timeout(5000)
    @Retry(maxRetries = 3, delay = 100)
    @Fallback(CustomerSupportAgentFallback.class)
    String chat(String userMessage);

    public static class CustomerSupportAgentFallback implements FallbackHandler<String> {

        private static final String EMPTY_RESPONSE =
                "Failed to get a response from the AI Model. Are you sure it's up and running, and configured correctly?";

        @Override
        public String handle(ExecutionContext context) {
            return EMPTY_RESPONSE;
        }
    }
}
