#include "LLMProviderFactory.hpp"
#include "ClaudeProvider.hpp"
#include "OpenAIProvider.hpp"
#include <spdlog/spdlog.h>
#include <stdexcept>

namespace celuna {

std::unique_ptr<ILLMProvider> LLMProviderFactory::create(const nlohmann::json& config) {
    std::string providerName = config.value("provider", "claude");

    if (!config.contains("providers") || !config["providers"].contains(providerName)) {
        throw std::runtime_error("Provider configuration not found: " + providerName);
    }

    return createProvider(providerName, config["providers"][providerName]);
}

std::unique_ptr<ILLMProvider> LLMProviderFactory::createProvider(
    const std::string& providerName,
    const nlohmann::json& providerConfig) {

    auto logger = spdlog::get("LLMFactory");
    if (!logger) {
        logger = spdlog::stdout_color_mt("LLMFactory");
    }

    logger->info("Creating LLM provider: {}", providerName);

    if (providerName == "claude") {
        return std::make_unique<ClaudeProvider>(providerConfig);
    }
    else if (providerName == "openai") {
        return std::make_unique<OpenAIProvider>(providerConfig);
    }
    // Future providers:
    // else if (providerName == "ollama") {
    //     return std::make_unique<OllamaProvider>(providerConfig);
    // }
    // else if (providerName == "deepseek") { ... }
    // else if (providerName == "gemini") { ... }

    throw std::runtime_error("Unknown LLM provider: " + providerName);
}

} // namespace celuna
