/***************************************************************************
 * SPDX-FileCopyrightText: 2026 S. MANKOWSKI stephane@mankowski.fr
 * SPDX-License-Identifier: GPL-3.0-or-later
 ***************************************************************************/
/** @file
 * This file is Skrooge plugin for AI assistant.
 *
 * @author Stephane MANKOWSKI
 */
#include "skgassistantplugin.h"

#include <kactioncollection.h>
#include <kpluginfactory.h>

#include <qaction.h>
#include <qdockwidget.h>

#include "skgassistant_settings.h"
#include "skgassistantplugindockwidget.h"
#include "skgassistantpluginwidget.h"
#include "skgmainpanel.h"
#include "skgtraces.h"

/**
 * This plugin factory.
 */
K_PLUGIN_CLASS_WITH_JSON(SKGAssistantPlugin, "metadata.json")

SKGAssistantPlugin::SKGAssistantPlugin(QWidget *iWidget, QObject *iParent, const KPluginMetaData & /*metaData*/, const QVariantList & /*iArg*/)
    : SKGInterfacePlugin(iParent)
    , m_currentDocument(nullptr)
    , m_dockWidget(nullptr)
{
    Q_UNUSED(iWidget)
    SKGTRACEINFUNC(10)
}

SKGAssistantPlugin::~SKGAssistantPlugin()
{
    SKGTRACEINFUNC(10)
    m_currentDocument = nullptr;
    m_dockWidget = nullptr;
}

bool SKGAssistantPlugin::setupActions(SKGDocument *iDocument)
{
    SKGTRACEINFUNC(10)

    m_currentDocument = iDocument;
    if (m_currentDocument == nullptr) {
        return false;
    }

    setComponentName(QStringLiteral("skrooge_aiassistant"), title());
    setXMLFile(QStringLiteral("skrooge_aiassistant.rc"));

    auto dockContent = new SKGAssistantPluginDockWidget(SKGMainPanel::getMainPanel(), m_currentDocument);
    if (dockContent != nullptr) {
        m_dockWidget = new QDockWidget(SKGMainPanel::getMainPanel());
        if (m_dockWidget != nullptr) {
            m_dockWidget->setObjectName(QStringLiteral("skg_aiassistant_docwidget"));
            m_dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
            m_dockWidget->setWindowTitle(title());
            m_dockWidget->setWidget(dockContent);

            QAction *toggle = m_dockWidget->toggleViewAction();
            QAction *panelAction = actionCollection()->addAction(QStringLiteral("view_aiassistant"));
            registerGlobalAction(QStringLiteral("view_aiassistant"), panelAction);
            panelAction->setCheckable(true);
            panelAction->setChecked(toggle->isChecked());
            panelAction->setText(toggle->text());
            connect(panelAction, &QAction::triggered, toggle, &QAction::trigger);
            connect(toggle, &QAction::toggled, panelAction, &QAction::setChecked);
        }
    }

    return true;
}

SKGTabPage *SKGAssistantPlugin::getWidget()
{
    SKGTRACEINFUNC(10)
    return new SKGAssistantPluginWidget(SKGMainPanel::getMainPanel(), m_currentDocument);
}

QDockWidget *SKGAssistantPlugin::getDockWidget()
{
    SKGTRACEINFUNC(10)
    return m_dockWidget;
}

QWidget *SKGAssistantPlugin::getPreferenceWidget()
{
    SKGTRACEINFUNC(10)
    QWidget *w = new QWidget();
    ui.setupUi(w);
    QStringList models = QStringList() << QStringLiteral("google/gemma-4-12b-qat") << QStringLiteral("qwen/qwen2.5-coder-14b")
                                       << QStringLiteral("qwen/qwen3.5-9b");
    ui.kLabel->setText(i18nc("Label for the AI assistant plugin",
                             "<p><strong>Dickens</strong> is your AI companion. To enable it, you must specify an <strong>OpenAI</strong>-compatible server. "
                             "You can use a remote server, but for reasons of confidentiality regarding your sensitive data, it is recommended to use a local "
                             "one.</p>Example of working well local models:<br>%1",
                             models.join(QStringLiteral(", "))));
    return w;
}

KConfigSkeleton *SKGAssistantPlugin::getPreferenceSkeleton()
{
    SKGTRACEINFUNC(10)
    return skgassistant_settings::self();
}

QString SKGAssistantPlugin::title() const
{
    return i18nc("Noun, something used to assist the user", "Dickens - AI Assistant");
}

QString SKGAssistantPlugin::icon() const
{
    return QStringLiteral("qa");
}

int SKGAssistantPlugin::getOrder() const
{
    return 200;
}

bool SKGAssistantPlugin::isInPagesChooser() const
{
    return true;
}

#include "skgassistantplugin.moc"
