package ai.decompile.ai.service;

import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.ai.chat.model.StreamingChatModel;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

@Log4j2
@Component
@RequiredArgsConstructor
@ConditionalOnProperty(name = "app.ai.enabled", havingValue = "true", matchIfMissing = true)
public class OllamaStreamingModelProvider implements StreamingModelProvider {
  private final ObjectProvider<OllamaChatModel> ollamaChatModelProvider;

  @Override
  public StreamingChatModel resolve() {
    var ollamaModel = ollamaChatModelProvider.getIfAvailable();
    if (Objects.isNull(ollamaModel)) {
      throw new IllegalStateException(
          "Ollama is not available. Install ollama and run 'ollama pull gemma4:12b'.");
    }

    return ollamaModel;
  }
}
