package com.example.cuida.services;

import com.google.ai.client.generativeai.GenerativeModel;
import com.google.ai.client.generativeai.java.GenerativeModelFutures;
import com.google.ai.client.generativeai.type.Content;
import com.google.ai.client.generativeai.type.GenerateContentResponse;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class Gemini {

    private final GenerativeModelFutures modelo;

    public Gemini() {
        // 1. Configurar o modelo (usa a tua API Key do Google AI Studio)
        GenerativeModel generativeModel = new GenerativeModel(
                "gemini-2.5-flash",
                "AIzaSyBmLgn-SHaTDvAeDWsw2iTZRR9gahhOu7k");
        this.modelo = GenerativeModelFutures.from(generativeModel);
    }

    public interface GeminiCallback {
        void onSuccess(String result);

        void onError(Throwable t);
    }

    public void fazerPergunta(String promptUtilizador, GeminiCallback callback) {
        // 2. Preparar o conteúdo da pergunta
        Content conteudo = new Content.Builder()
                .addText(promptUtilizador)
                .build();

        // 3. Chamar a IA de forma assíncrona
        ListenableFuture<GenerateContentResponse> respostaFuture = modelo.generateContent(conteudo);

        Executor executor = Executors.newSingleThreadExecutor();

        Futures.addCallback(respostaFuture, new FutureCallback<GenerateContentResponse>() {
            @Override
            public void onSuccess(GenerateContentResponse resultado) {
                // Aqui recebes o texto da IA
                String textoResposta = resultado.getText();
                callback.onSuccess(textoResposta);
            }

            @Override
            public void onFailure(Throwable t) {
                callback.onError(t);
            }
        }, executor);
    }
}
