/*
  SPDX-FileCopyrightText: 2025-2026 Laurent Montel <montel@kde.org>

  SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "genericnetworkserverinfo.h"

#include "autogeneratetext_genericnetwork_debug.h"

#include <KLocalizedString>

using namespace Qt::Literals::StringLiterals;
GenericNetworkServerInfo::GenericNetworkServerInfo() = default;

GenericNetworkServerInfo::~GenericNetworkServerInfo() = default;

QString GenericNetworkServerInfo::translatedName(GenericNetworkManager::PluginNetworkType type)
{
    switch (type) {
    case GenericNetworkManager::PluginNetworkType::Unknown:
        qCWarning(AUTOGENERATETEXT_GENERICNETWORK_LOG) << "PluginNetworkType is unknown. It's a bug";
        return {};
    case GenericNetworkManager::PluginNetworkType::MistralAI:
        return i18n("Mistral AI");
    case GenericNetworkManager::PluginNetworkType::NovitaAI:
        return i18n("Novita AI");
    case GenericNetworkManager::PluginNetworkType::OpenAI:
        return i18n("OpenAI");
    case GenericNetworkManager::PluginNetworkType::KlusterAI:
        return i18n("Kluster AI");
    case GenericNetworkManager::PluginNetworkType::GroqAI:
        return i18n("Groq Cloud");
    case GenericNetworkManager::PluginNetworkType::CerebrasAI:
        return i18n("Cerebras AI");
    case GenericNetworkManager::PluginNetworkType::VeniceAI:
        return i18n("Venice");
    case GenericNetworkManager::PluginNetworkType::LlamaApi:
        return i18n("Llama Api");
    case GenericNetworkManager::PluginNetworkType::Anthropic:
        return i18n("Anthropic");
    case GenericNetworkManager::PluginNetworkType::KimiAI:
        return i18n("Kimi (Moonshot AI)");
    case GenericNetworkManager::PluginNetworkType::GrokAI:
        return i18n("Grok (X.ai)");
    case GenericNetworkManager::PluginNetworkType::Qwen:
        return i18n("Qwen (DashScope)");
    case GenericNetworkManager::PluginNetworkType::DeepInfra:
        return i18n("DeepInfra");
    case GenericNetworkManager::PluginNetworkType::ChatGPT:
        return i18n("OpenAI ChatGPT");
    case GenericNetworkManager::PluginNetworkType::CompactifAI:
        return i18n("CompactifAI");
    case GenericNetworkManager::PluginNetworkType::OpenRouterAI:
        return i18n("OpenRouter AI");
    case GenericNetworkManager::PluginNetworkType::SarvamAI:
        return i18n("Sarvam AI");
    case GenericNetworkManager::PluginNetworkType::AtlasCloud:
        return i18n("AtlasCloud OpenAI-compatible API");
    case GenericNetworkManager::PluginNetworkType::Gemini:
        return i18n("Google Gemini");
    }
    return {};
}

QString GenericNetworkServerInfo::responsesPath(GenericNetworkManager::PluginNetworkType type)
{
    switch (type) {
    case GenericNetworkManager::PluginNetworkType::Unknown:
        qCWarning(AUTOGENERATETEXT_GENERICNETWORK_LOG) << "PluginNetworkType is unknown. It's a bug";
        return {};
    case GenericNetworkManager::PluginNetworkType::MistralAI:
    case GenericNetworkManager::PluginNetworkType::OpenAI:
    case GenericNetworkManager::PluginNetworkType::KlusterAI:
    case GenericNetworkManager::PluginNetworkType::GroqAI:
    case GenericNetworkManager::PluginNetworkType::VeniceAI:
    case GenericNetworkManager::PluginNetworkType::LlamaApi:
    case GenericNetworkManager::PluginNetworkType::CerebrasAI:
    case GenericNetworkManager::PluginNetworkType::Anthropic:
    case GenericNetworkManager::PluginNetworkType::KimiAI:
    case GenericNetworkManager::PluginNetworkType::GrokAI:
    case GenericNetworkManager::PluginNetworkType::Qwen:
    case GenericNetworkManager::PluginNetworkType::NovitaAI:
    case GenericNetworkManager::PluginNetworkType::DeepInfra:
    case GenericNetworkManager::PluginNetworkType::ChatGPT:
    case GenericNetworkManager::PluginNetworkType::CompactifAI:
    case GenericNetworkManager::PluginNetworkType::OpenRouterAI:
    case GenericNetworkManager::PluginNetworkType::SarvamAI:
    case GenericNetworkManager::PluginNetworkType::AtlasCloud:
    case GenericNetworkManager::PluginNetworkType::Gemini:
        return u"responses"_s;
    }
    return {};
}

QString GenericNetworkServerInfo::chatCompletionPath(GenericNetworkManager::PluginNetworkType type)
{
    switch (type) {
    case GenericNetworkManager::PluginNetworkType::Unknown:
        qCWarning(AUTOGENERATETEXT_GENERICNETWORK_LOG) << "PluginNetworkType is unknown. It's a bug";
        return {};
    case GenericNetworkManager::PluginNetworkType::MistralAI:
    case GenericNetworkManager::PluginNetworkType::OpenAI:
    case GenericNetworkManager::PluginNetworkType::KlusterAI:
    case GenericNetworkManager::PluginNetworkType::GroqAI:
    case GenericNetworkManager::PluginNetworkType::VeniceAI:
    case GenericNetworkManager::PluginNetworkType::LlamaApi:
    case GenericNetworkManager::PluginNetworkType::CerebrasAI:
    case GenericNetworkManager::PluginNetworkType::Anthropic:
    case GenericNetworkManager::PluginNetworkType::KimiAI:
    case GenericNetworkManager::PluginNetworkType::GrokAI:
    case GenericNetworkManager::PluginNetworkType::Qwen:
    case GenericNetworkManager::PluginNetworkType::NovitaAI:
    case GenericNetworkManager::PluginNetworkType::DeepInfra:
    case GenericNetworkManager::PluginNetworkType::ChatGPT:
    case GenericNetworkManager::PluginNetworkType::CompactifAI:
    case GenericNetworkManager::PluginNetworkType::OpenRouterAI:
    case GenericNetworkManager::PluginNetworkType::SarvamAI:
    case GenericNetworkManager::PluginNetworkType::AtlasCloud:
    case GenericNetworkManager::PluginNetworkType::Gemini:
        return u"chat/completions"_s;
    }
    return {};
}

QString GenericNetworkServerInfo::webSite(GenericNetworkManager::PluginNetworkType type)
{
    switch (type) {
    case GenericNetworkManager::PluginNetworkType::Unknown:
        qCWarning(AUTOGENERATETEXT_GENERICNETWORK_LOG) << "PluginNetworkType is unknown. It's a bug";
        return {};
    case GenericNetworkManager::PluginNetworkType::MistralAI:
        return u"https://mistral.ai/"_s;
    case GenericNetworkManager::PluginNetworkType::OpenAI:
        return u"https://openai.com/"_s;
    case GenericNetworkManager::PluginNetworkType::KlusterAI:
        return u"https://www.kluster.ai/"_s;
    case GenericNetworkManager::PluginNetworkType::GroqAI:
        return u"https://groq.com/"_s;
    case GenericNetworkManager::PluginNetworkType::CerebrasAI:
        return u"https://www.cerebras.ai/"_s;
    case GenericNetworkManager::PluginNetworkType::VeniceAI:
        return u"https://www.venice.ai/"_s;
    case GenericNetworkManager::PluginNetworkType::LlamaApi:
        return u"https://www.llama.com/products/llama-api/"_s;
    case GenericNetworkManager::PluginNetworkType::Anthropic:
        return u"https://www.anthropic.com/"_s;
    case GenericNetworkManager::PluginNetworkType::KimiAI:
        return u"https://www.moonshot.ai/"_s;
    case GenericNetworkManager::PluginNetworkType::GrokAI:
        return u"https://api.x.ai/"_s;
    case GenericNetworkManager::PluginNetworkType::Qwen:
        return u"https://qwen.ai/home"_s;
    case GenericNetworkManager::PluginNetworkType::NovitaAI:
        return u"https://novita.ai/"_s;
    case GenericNetworkManager::PluginNetworkType::DeepInfra:
        return u"https://deepinfra.com/"_s;
    case GenericNetworkManager::PluginNetworkType::ChatGPT:
        return u"https://chatgpt.com/"_s;
    case GenericNetworkManager::PluginNetworkType::CompactifAI:
        return u"https://docs.compactif.ai/"_s;
    case GenericNetworkManager::PluginNetworkType::OpenRouterAI:
        return u"https://openrouter.ai/"_s;
    case GenericNetworkManager::PluginNetworkType::SarvamAI:
        return u"https://api.sarvam.ai/"_s;
    case GenericNetworkManager::PluginNetworkType::AtlasCloud:
        return u"https://www.atlascloud.ai/"_s;
    case GenericNetworkManager::PluginNetworkType::Gemini:
        return u"https://gemini.google.com"_s;
    }
    return {};
}

QString GenericNetworkServerInfo::apiUrl(GenericNetworkManager::PluginNetworkType type)
{
    switch (type) {
    case GenericNetworkManager::PluginNetworkType::Unknown:
        qCWarning(AUTOGENERATETEXT_GENERICNETWORK_LOG) << "PluginNetworkType is unknown. It's a bug";
        return {};
    case GenericNetworkManager::PluginNetworkType::MistralAI:
        return u"https://api.mistral.ai/v1/"_s;
    case GenericNetworkManager::PluginNetworkType::OpenAI:
        return u"https://api.openai.com/v1/"_s;
    case GenericNetworkManager::PluginNetworkType::KlusterAI:
        return u"https://api.kluster.ai/v1/"_s;
    case GenericNetworkManager::PluginNetworkType::GroqAI:
        return u"https://api.groq.com/openai/v1/"_s;
    case GenericNetworkManager::PluginNetworkType::CerebrasAI:
        return u"https://api.cerebras.ai/v1/"_s;
    case GenericNetworkManager::PluginNetworkType::VeniceAI:
        return u"https://api.venice.ai/api/v1/"_s;
    case GenericNetworkManager::PluginNetworkType::LlamaApi:
        return u"https://api.llama.com/compat/v1/"_s;
    case GenericNetworkManager::PluginNetworkType::Anthropic:
        return u"https://api.anthropic.com/v1/"_s;
    case GenericNetworkManager::PluginNetworkType::KimiAI:
        return u"https://api.moonshot.ai/v1/"_s;
    case GenericNetworkManager::PluginNetworkType::GrokAI:
        return u"https://api.x.ai/v1/"_s;
    case GenericNetworkManager::PluginNetworkType::Qwen:
        return u"https://dashscope-intl.aliyuncs.com/compatible-mode/v1/"_s;
    case GenericNetworkManager::PluginNetworkType::NovitaAI:
        return u"https://api.novita.ai/v3/openai/"_s;
    case GenericNetworkManager::PluginNetworkType::DeepInfra:
        return u"https://api.deepinfra.com/v1/openai/"_s;
    case GenericNetworkManager::PluginNetworkType::ChatGPT:
        return u"https://api.openai.com/v1/"_s;
    case GenericNetworkManager::PluginNetworkType::CompactifAI:
        return u"https://your-compactifai-api-endpoint/v1/"_s;
    case GenericNetworkManager::PluginNetworkType::OpenRouterAI:
        return u"https://openrouter.ai/api/v1/"_s;
    case GenericNetworkManager::PluginNetworkType::SarvamAI:
        return u"https://api.sarvam.ai/v1/"_s;
    case GenericNetworkManager::PluginNetworkType::AtlasCloud:
        return u"https://api.atlascloud.ai/v1/"_s;
    case GenericNetworkManager::PluginNetworkType::Gemini:
        return u"https://generativelanguage.googleapis.com/v1beta/openai/"_s;
    }
    return {};
}

QString GenericNetworkServerInfo::description(GenericNetworkManager::PluginNetworkType type)
{
    switch (type) {
    case GenericNetworkManager::PluginNetworkType::Unknown:
        qCWarning(AUTOGENERATETEXT_GENERICNETWORK_LOG) << "PluginNetworkType is unknown. It's a bug";
        return {};
    case GenericNetworkManager::PluginNetworkType::MistralAI:
        return i18n("Mistral AI large language models");
    case GenericNetworkManager::PluginNetworkType::OpenAI:
    case GenericNetworkManager::PluginNetworkType::VeniceAI:
    case GenericNetworkManager::PluginNetworkType::Anthropic:
    case GenericNetworkManager::PluginNetworkType::GrokAI:
    case GenericNetworkManager::PluginNetworkType::ChatGPT:
    case GenericNetworkManager::PluginNetworkType::Gemini:
        return {};
    case GenericNetworkManager::PluginNetworkType::GroqAI:
        return i18n("Groq AI");
    case GenericNetworkManager::PluginNetworkType::KlusterAI:
        return i18n("Kluster AI cloud inference API");
    case GenericNetworkManager::PluginNetworkType::CerebrasAI:
        return i18n("Cerebras AI cloud inference API");
    case GenericNetworkManager::PluginNetworkType::LlamaApi:
        return i18n("Meta AI Llama API");
    case GenericNetworkManager::PluginNetworkType::KimiAI:
        return i18n("Kimi large language models by Moonshot AI");
    case GenericNetworkManager::PluginNetworkType::Qwen:
        return i18n("Alibaba Cloud Qwen large language models via DashScope");
    case GenericNetworkManager::PluginNetworkType::NovitaAI:
        return i18n("Novita AI cloud inference API");
    case GenericNetworkManager::PluginNetworkType::DeepInfra:
        return i18n("DeepInfra cloud inference API");
    case GenericNetworkManager::PluginNetworkType::CompactifAI:
        return i18n("CompactifAI");
    case GenericNetworkManager::PluginNetworkType::OpenRouterAI:
        return i18n("OpenRouter AI");
    case GenericNetworkManager::PluginNetworkType::SarvamAI:
        return i18n("Sarvam AI instance");
    case GenericNetworkManager::PluginNetworkType::AtlasCloud:
        return i18n("AtlasCloud OpenAI-compatible API");
    }
    return {};
}

QString GenericNetworkServerInfo::pluginName(GenericNetworkManager::PluginNetworkType type)
{
    switch (type) {
    case GenericNetworkManager::PluginNetworkType::Unknown:
        qCWarning(AUTOGENERATETEXT_GENERICNETWORK_LOG) << "PluginNetworkType is unknown. It's a bug";
        return {};
    case GenericNetworkManager::PluginNetworkType::MistralAI:
        return u"mistralai"_s;
    case GenericNetworkManager::PluginNetworkType::OpenAI:
        return u"openai"_s;
    case GenericNetworkManager::PluginNetworkType::KlusterAI:
        return u"klusterai"_s;
    case GenericNetworkManager::PluginNetworkType::GroqAI:
        return u"groqai"_s;
    case GenericNetworkManager::PluginNetworkType::CerebrasAI:
        return u"cerebrasai"_s;
    case GenericNetworkManager::PluginNetworkType::VeniceAI:
        return u"veniceai"_s;
    case GenericNetworkManager::PluginNetworkType::LlamaApi:
        return u"llamaapi"_s;
    case GenericNetworkManager::PluginNetworkType::Anthropic:
        return u"anthropic"_s;
    case GenericNetworkManager::PluginNetworkType::KimiAI:
        return u"kimiai"_s;
    case GenericNetworkManager::PluginNetworkType::GrokAI:
        return u"grokai"_s;
    case GenericNetworkManager::PluginNetworkType::Qwen:
        return u"qwen"_s;
    case GenericNetworkManager::PluginNetworkType::NovitaAI:
        return u"novitaai"_s;
    case GenericNetworkManager::PluginNetworkType::DeepInfra:
        return u"deepinfra"_s;
    case GenericNetworkManager::PluginNetworkType::ChatGPT:
        return u"chatgpt"_s;
    case GenericNetworkManager::PluginNetworkType::CompactifAI:
        return u"compactifai"_s;
    case GenericNetworkManager::PluginNetworkType::OpenRouterAI:
        return u"openrouterai"_s;
    case GenericNetworkManager::PluginNetworkType::SarvamAI:
        return u"sarvamai"_s;
    case GenericNetworkManager::PluginNetworkType::AtlasCloud:
        return u"atlascloudai"_s;
    case GenericNetworkManager::PluginNetworkType::Gemini:
        return u"gemini"_s;
    }
    return {};
}

GenericNetworkManager::PluginNetworkType GenericNetworkServerInfo::pluginNetworkTypeFromString(const QString &str)
{
    if (str == QLatin1StringView("mistralai")) {
        return GenericNetworkManager::PluginNetworkType::MistralAI;
    } else if (str == QLatin1StringView("openai")) {
        return GenericNetworkManager::PluginNetworkType::OpenAI;
    } else if (str == QLatin1StringView("klusterai")) {
        return GenericNetworkManager::PluginNetworkType::KlusterAI;
    } else if (str == QLatin1StringView("groqai")) {
        return GenericNetworkManager::PluginNetworkType::GroqAI;
    } else if (str == QLatin1StringView("cerebrasai")) {
        return GenericNetworkManager::PluginNetworkType::CerebrasAI;
    } else if (str == QLatin1StringView("veniceai")) {
        return GenericNetworkManager::PluginNetworkType::VeniceAI;
    } else if (str == QLatin1StringView("llamaapi")) {
        return GenericNetworkManager::PluginNetworkType::LlamaApi;
    } else if (str == QLatin1StringView("anthropic")) {
        return GenericNetworkManager::PluginNetworkType::Anthropic;
    } else if (str == QLatin1StringView("kimiai")) {
        return GenericNetworkManager::PluginNetworkType::KimiAI;
    } else if (str == QLatin1StringView("grokai")) {
        return GenericNetworkManager::PluginNetworkType::GrokAI;
    } else if (str == QLatin1StringView("qwen")) {
        return GenericNetworkManager::PluginNetworkType::Qwen;
    } else if (str == QLatin1StringView("novitaai")) {
        return GenericNetworkManager::PluginNetworkType::NovitaAI;
    } else if (str == QLatin1StringView("deepinfra")) {
        return GenericNetworkManager::PluginNetworkType::DeepInfra;
    } else if (str == QLatin1StringView("chatgpt")) {
        return GenericNetworkManager::PluginNetworkType::ChatGPT;
    } else if (str == QLatin1StringView("compactifai")) {
        return GenericNetworkManager::PluginNetworkType::CompactifAI;
    } else if (str == QLatin1StringView("openrouterai")) {
        return GenericNetworkManager::PluginNetworkType::OpenRouterAI;
    } else if (str == QLatin1StringView("sarvamai")) {
        return GenericNetworkManager::PluginNetworkType::SarvamAI;
    } else if (str == QLatin1StringView("atlascloudai")) {
        return GenericNetworkManager::PluginNetworkType::AtlasCloud;
    } else if (str == QLatin1StringView("gemini")) {
        return GenericNetworkManager::PluginNetworkType::Gemini;
    } else {
        qCWarning(AUTOGENERATETEXT_GENERICNETWORK_LOG) << "PluginNetworkType is unknown. It's a bug" << str;
        return GenericNetworkManager::PluginNetworkType::Unknown;
    }
}

QList<TextAutoGenerateText::TextAutoGenerateTextClient::SupportedServer> GenericNetworkServerInfo::supportedServers(const QString &name)
{
    QList<TextAutoGenerateText::TextAutoGenerateTextClient::SupportedServer> listInfo;
    for (int i = static_cast<int>(GenericNetworkManager::PluginNetworkType::Unknown) + 1;
         i <= static_cast<int>(GenericNetworkManager::PluginNetworkType::LastElement);
         ++i) {
        const TextAutoGenerateText::TextAutoGenerateTextClient::SupportedServer info{
            .localizedName = QStringLiteral("%1 (%2)").arg(
                translatedName(static_cast<GenericNetworkManager::PluginNetworkType>(i)),
                TextAutoGenerateText::TextAutoGenerateTextPlugin::convertEngineType(TextAutoGenerateText::TextAutoGenerateTextPlugin::EngineType::Network)),
            .identifier = pluginName(static_cast<GenericNetworkManager::PluginNetworkType>(i)),
            .pluginName = name,
        };
        listInfo.append(info);
    }
    return listInfo;
}

GenericNetworkManager::Limitations GenericNetworkServerInfo::limitations(GenericNetworkManager::PluginNetworkType type)
{
    GenericNetworkManager::Limitations limits;
    limits |= GenericNetworkManager::Limitation::None;
    switch (type) {
    case GenericNetworkManager::PluginNetworkType::Unknown:
        qCWarning(AUTOGENERATETEXT_GENERICNETWORK_LOG) << "PluginNetworkType is unknown. It's a bug";
        return limits;
    case GenericNetworkManager::PluginNetworkType::MistralAI:
    case GenericNetworkManager::PluginNetworkType::LlamaApi:
    case GenericNetworkManager::PluginNetworkType::OpenAI:
        return limits;
    case GenericNetworkManager::PluginNetworkType::GroqAI:
        limits |= GenericNetworkManager::Limitation::TextOnly;
        return limits;
    case GenericNetworkManager::PluginNetworkType::VeniceAI:
        limits |= GenericNetworkManager::Limitation::NoSystemMessage;
        return limits;
    case GenericNetworkManager::PluginNetworkType::KimiAI:
    case GenericNetworkManager::PluginNetworkType::NovitaAI:
        limits |= GenericNetworkManager::Limitation::NoSeed;
        return limits;
    case GenericNetworkManager::PluginNetworkType::ChatGPT:
        limits |= GenericNetworkManager::Limitation::UseMaxCompletionTokens;
        return limits;
    case GenericNetworkManager::PluginNetworkType::KlusterAI:
    case GenericNetworkManager::PluginNetworkType::CerebrasAI:
    case GenericNetworkManager::PluginNetworkType::Anthropic:
    case GenericNetworkManager::PluginNetworkType::Gemini:
    case GenericNetworkManager::PluginNetworkType::GrokAI:
    case GenericNetworkManager::PluginNetworkType::Qwen:
    case GenericNetworkManager::PluginNetworkType::DeepInfra:
    case GenericNetworkManager::PluginNetworkType::CompactifAI:
    case GenericNetworkManager::PluginNetworkType::OpenRouterAI:
    case GenericNetworkManager::PluginNetworkType::SarvamAI:
    case GenericNetworkManager::PluginNetworkType::AtlasCloud:
        return limits;
    }
    return limits;
}

#include "moc_genericnetworkserverinfo.cpp"
