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

  SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "ollamacommonsettings.h"
#include <KConfigGroup>
#include <QDebug>

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

OllamaCommonSettings::~OllamaCommonSettings() = default;

int OllamaCommonSettings::seed() const
{
    return mSeed;
}

void OllamaCommonSettings::setSeed(int newSeed)
{
    mSeed = newSeed;
}

int OllamaCommonSettings::keepAliveMinutes() const
{
    return mKeepAliveMinutes;
}

void OllamaCommonSettings::setKeepAliveMinutes(int newKeepAliveMinutes)
{
    mKeepAliveMinutes = newKeepAliveMinutes;
}

int OllamaCommonSettings::defaultSeed() const
{
    return 0;
}

int OllamaCommonSettings::defaultKeepAliveMinutes() const
{
    return 1;
}

qint64 OllamaCommonSettings::defaultContextWindowSize() const
{
    return 16384;
}

bool OllamaCommonSettings::defaultThoughtProcessing() const
{
    return false;
}

OllamaCommonSettings::KeepAliveType OllamaCommonSettings::defaultKeepAliveType() const
{
    return OllamaCommonSettings::KeepAliveType::UnloadAfterUse;
}

void OllamaCommonSettings::load(const KConfigGroup &config)
{
    PluginCommonSettings::load(config);
    setSeed(config.readEntry(u"Seed"_s, defaultSeed()));
    setKeepAliveMinutes(config.readEntry(u"KeepAliveMinutes"_s, defaultKeepAliveMinutes()));
    setContextWindowSize(config.readEntry(u"ContextWindowSize"_s, defaultContextWindowSize()));
    setThoughtProcessing(config.readEntry(u"ThoughtProcessing"_s, defaultThoughtProcessing()));
    setKeepAliveType(OllamaCommonSettings::convertKeepAliveTypeFromString(
        config.readEntry(u"KeepAliveType"_s, OllamaCommonSettings::convertKeepAliveTypeToString(defaultKeepAliveType()))));
    setShareNameType(PluginCommonSettings::convertShareNameTypeFromString(
        config.readEntry(u"ShareName"_s, PluginCommonSettings::convertShareNameTypeToString(defaultShareNameType()))));
}

void OllamaCommonSettings::save(KConfigGroup &config)
{
    PluginCommonSettings::save(config);
    config.writeEntry(u"Seed"_s, seed());
    config.writeEntry(u"KeepAliveMinutes"_s, keepAliveMinutes());
    config.writeEntry(u"ContextWindowSize"_s, contextWindowSize());
    config.writeEntry(u"KeepAliveType"_s, OllamaCommonSettings::convertKeepAliveTypeToString(mKeepAliveType));
    config.writeEntry(u"ThoughtProcessing"_s, thoughtProcessing());
    config.writeEntry(u"ShareName"_s, PluginCommonSettings::convertShareNameTypeToString(shareNameType()));
}

OllamaCommonSettings::KeepAliveType OllamaCommonSettings::keepAliveType() const
{
    return mKeepAliveType;
}

void OllamaCommonSettings::setKeepAliveType(OllamaCommonSettings::KeepAliveType newKeepAliveType)
{
    mKeepAliveType = newKeepAliveType;
}

QDebug operator<<(QDebug d, const OllamaCommonSettings &t)
{
    d.space() << "displayName:" << t.displayName();
    d.space() << "currentModel:" << t.currentModel();
    d.space() << "temperature:" << t.temperature();
    d.space() << "seed:" << t.seed();
    d.space() << "keepAliveMinutes:" << t.keepAliveMinutes();
    d.space() << "keepAliveType:" << t.keepAliveType();
    d.space() << "contextWindowSize:" << t.contextWindowSize();
    d.space() << "thoughtProcessing:" << t.thoughtProcessing();
    d.space() << "shareNameType:" << t.shareNameType();
    return d;
}

QString OllamaCommonSettings::convertKeepAliveTypeToString(OllamaCommonSettings::KeepAliveType type)
{
    switch (type) {
    case KeepAliveType::Unknown:
        break;
    case KeepAliveType::KeepAliveForever:
        return u"KeepAliveForever"_s;
    case KeepAliveType::UnloadAfterUse:
        return u"UnloadAfterUse"_s;
    case KeepAliveType::SetTimer:
        return u"SetTimer"_s;
    }

    return {};
}

OllamaCommonSettings::KeepAliveType OllamaCommonSettings::convertKeepAliveTypeFromString(const QString &str)
{
    if (str == "KeepAliveForever"_L1) {
        return OllamaCommonSettings::KeepAliveType::KeepAliveForever;
    } else if (str == "UnloadAfterUse"_L1) {
        return OllamaCommonSettings::KeepAliveType::UnloadAfterUse;
    } else if (str == "SetTimer"_L1) {
        return OllamaCommonSettings::KeepAliveType::SetTimer;
    }
    return OllamaCommonSettings::KeepAliveType::Unknown;
}

qint64 OllamaCommonSettings::contextWindowSize() const
{
    return mContextWindowSize;
}

void OllamaCommonSettings::setContextWindowSize(qint64 newContextWindowSize)
{
    mContextWindowSize = newContextWindowSize;
}

bool OllamaCommonSettings::operator==(const OllamaCommonSettings &other) const
{
    return other.mDisplayName == mDisplayName && other.mCurrentModel == mCurrentModel && mSeed == other.mSeed && mTemperature == other.mTemperature
        && mKeepAliveMinutes == other.mKeepAliveMinutes && mContextWindowSize == other.mContextWindowSize && mKeepAliveType == other.mKeepAliveType
        && mShareNameType == other.mShareNameType && mThoughtProcessing == other.mThoughtProcessing;
}

#include "moc_ollamacommonsettings.cpp"
