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

  SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "mcpprotocolserver.h"
#include "common/mcpprotocolplugininterface.h"
#include "textautogeneratetextmcpprotocol_core_debug.h"
#include <KPluginFactory>
#include <KPluginMetaData>

using namespace TextAutoGenerateTextMcpProtocolCore;
using namespace Qt::Literals::StringLiterals;

McpProtocolServer::McpProtocolServer(McpProtocolPlugin::TransportType protocolType, QObject *parent)
    : QObject{parent}
    , mProtocolType(protocolType)
{
    loadPlugin();
}

McpProtocolServer::~McpProtocolServer() = default;

void McpProtocolServer::loadPlugin()
{
    const KPluginMetaData serverPlugin(u"kf6/textautogeneratetext/mcpprotocol/autogeneratetext_mcpprotocolserverplugin"_s);
    const auto result = KPluginFactory::instantiatePlugin<TextAutoGenerateTextMcpProtocolCore::McpProtocolPlugin>(serverPlugin, this);
    if (result) {
        mPlugin = result.plugin;
        initialize();
    } else {
        qCWarning(TEXTAUTOGENERATEMCPPROTOCOLCORE_LOG) << "Impossible to find  autogeneratetext_mcpprotocolserverplugin plugin";
    }
}

void McpProtocolServer::initialize()
{
    if (mPlugin) {
        mPluginInterface = mPlugin->createInterface(mProtocolType, this);
        if (mPluginInterface) {
            connect(mPluginInterface, &McpProtocolPluginInterface::error, this, &McpProtocolServer::error);
            connect(mPluginInterface, &McpProtocolPluginInterface::received, this, &McpProtocolServer::received);
            connect(mPluginInterface, &McpProtocolPluginInterface::started, this, &McpProtocolServer::started);
        }
    } else {
        qCWarning(TEXTAUTOGENERATEMCPPROTOCOLCORE_LOG) << "plugin not found for " << mProtocolType;
    }
}

void McpProtocolServer::setSettings(const McpProtocolSettings &settings)
{
    if (mPluginInterface) {
        mPluginInterface->setSettings(settings);
    } else {
        qCWarning(TEXTAUTOGENERATEMCPPROTOCOLCORE_LOG) << "mPluginInterface is nullptr. It's a bug";
    }
}

bool McpProtocolServer::canStart() const
{
    if (mPluginInterface) {
        const bool result = mPluginInterface->canStart();
        if (!result) {
            qCWarning(TEXTAUTOGENERATEMCPPROTOCOLCORE_LOG) << "Impossible to start client. Missing McpProtocolSettings. It's a bug";
        }
        return result;
    }
    qCWarning(TEXTAUTOGENERATEMCPPROTOCOLCORE_LOG) << "Impossible to start client. mPluginInterface is null. It's a bug";
    return false;
}

void McpProtocolServer::start()
{
    if (canStart()) {
        mPluginInterface->start();
    }
}

#include "moc_mcpprotocolserver.cpp"
