class LangchainLlmFactory
  VERTEXAI_PROJECT_ID = "norse-baton-398712".freeze
  VERTEXAI_LOCATION = "global".freeze

  def self.build(model_map:, temperature: 0.1, stream: true)
    case model_map.provider
    when "openrouter"
      openrouter_api_key = Rails.application.credentials.openrouter.api_key
      raise "OpenRouter API key is missing" if openrouter_api_key.blank?

      Langchain::LLM::OpenRouter.new(
        api_key: openrouter_api_key,
        default_options: {
          chat_model: model_map.llm_model_name,
          embedding_model: "google/gemini-embedding-001",
          temperature:,
          stream:
        }
      )
    when "openai"
      Langchain::LLM::OpenAI.new(
        api_key: Rails.application.credentials.openai_secret,
        default_options: { model: model_map.llm_model_name, temperature:, stream: }
      )
    when "vertexai"
      Langchain::LLM::SprintVertexAI.new(
        project_id: VERTEXAI_PROJECT_ID,
        region: VERTEXAI_LOCATION,
        default_options: {
          chat_model: model_map.llm_model_name,
          embedding_model: "gemini-embedding-001",
          dimensions: 3072,
          temperature:
        }
      )
    else
      raise "#{model_map.provider} is not supported by the LangChain RAG assistant yet"
    end
  end

  private_class_method :new
end
