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

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

#include "mcpprotocolresourcetemplate.h"
#include <QDebug>
#include <QJsonArray>
#include <QJsonObject>
using namespace Qt::Literals::StringLiterals;
using namespace TextAutoGenerateTextMcpProtocolCore;
McpProtocolResourceTemplate::McpProtocolResourceTemplate() = default;

McpProtocolResourceTemplate::~McpProtocolResourceTemplate() = default;

bool McpProtocolResourceTemplate::operator==(const McpProtocolResourceTemplate &other) const = default;

QDebug operator<<(QDebug d, const TextAutoGenerateTextMcpProtocolCore::McpProtocolResourceTemplate &t)
{
    d.space() << "meta:" << t.meta();
    d.space() << "annotations:" << t.annotations();
    d.space() << "description:" << t.description();
    d.space() << "mimeType:" << t.mimeType();
    d.space() << "icons:" << t.icons();
    d.space() << "name:" << t.name();
    d.space() << "title:" << t.title();
    d.space() << "uriTemplate:" << t.uriTemplate();
    return d;
}

McpProtocolResourceTemplate McpProtocolResourceTemplate::fromJson(const QJsonObject &obj)
{
    McpProtocolResourceTemplate prompt;
    if (obj.contains("_meta"_L1) && obj["_meta"_L1].isObject()) {
        prompt.setMeta(McpProtocolMeta::fromJson(obj["_meta"_L1].toObject()));
    }
    if (obj.contains("annotations"_L1) && obj["annotations"_L1].isObject()) {
        prompt.setAnnotations(McpProtocolAnnotations::fromJson(obj["annotations"_L1].toObject()));
    }
    if (obj.contains("description"_L1)) {
        prompt.setDescription(obj.value("description"_L1).toString());
    }
    if (obj.contains("mimeType"_L1)) {
        prompt.setMimeType(obj.value("mimeType"_L1).toString());
    }
    prompt.setName(obj.value("name"_L1).toString());
    if (obj.contains("title"_L1)) {
        prompt.setTitle(obj.value("title"_L1).toString());
    }
    prompt.setUriTemplate(obj.value("uriTemplate"_L1).toString());
    if (obj.contains("icons"_L1) && obj["icons"_L1].isArray()) {
        const QJsonArray arr = obj["icons"_L1].toArray();
        QList<McpProtocolIcon> list_icons;
        for (const auto &v : arr) {
            list_icons.append(McpProtocolIcon::fromJson(v.toObject()));
        }
        prompt.setIcons(list_icons);
    }
    return prompt;
}

QJsonObject McpProtocolResourceTemplate::toJson(const McpProtocolResourceTemplate &boolean)
{
    QJsonObject obj;
    obj["name"_L1] = boolean.name();
    obj["uriTemplate"_L1] = boolean.uriTemplate();
    if (boolean.meta().has_value()) {
        obj.insert("_meta"_L1, McpProtocolMeta::toJson(*boolean.meta()));
    }
    if (boolean.annotations().has_value()) {
        obj.insert("annotations"_L1, McpProtocolAnnotations::toJson(*boolean.annotations()));
    }
    if (boolean.description().has_value()) {
        obj.insert("description"_L1, *boolean.description());
    }
    if (boolean.icons().has_value()) {
        QJsonArray arr_icons;
        const auto icons = *boolean.icons();
        for (const auto &v : icons) {
            arr_icons.append(McpProtocolIcon::toJson(v));
        }
        obj.insert("icons"_L1, arr_icons);
    }
    if (boolean.mimeType().has_value()) {
        obj.insert("mimeType"_L1, *boolean.mimeType());
    }
    if (boolean.title().has_value()) {
        obj.insert("title"_L1, *boolean.title());
    }
    return obj;
}

std::optional<McpProtocolMeta> McpProtocolResourceTemplate::meta() const
{
    return mMeta;
}

void McpProtocolResourceTemplate::setMeta(std::optional<McpProtocolMeta> newMeta)
{
    mMeta = std::move(newMeta);
}

std::optional<McpProtocolAnnotations> McpProtocolResourceTemplate::annotations() const
{
    return mAnnotations;
}

void McpProtocolResourceTemplate::setAnnotations(std::optional<McpProtocolAnnotations> newAnnotations)
{
    mAnnotations = std::move(newAnnotations);
}

std::optional<QString> McpProtocolResourceTemplate::description() const
{
    return mDescription;
}

void McpProtocolResourceTemplate::setDescription(std::optional<QString> newDescription)
{
    mDescription = std::move(newDescription);
}

std::optional<QList<McpProtocolIcon>> McpProtocolResourceTemplate::icons() const
{
    return mIcons;
}

void McpProtocolResourceTemplate::setIcons(std::optional<QList<McpProtocolIcon>> newIcons)
{
    mIcons = std::move(newIcons);
}

std::optional<QString> McpProtocolResourceTemplate::mimeType() const
{
    return mMimeType;
}

void McpProtocolResourceTemplate::setMimeType(std::optional<QString> newMimeType)
{
    mMimeType = std::move(newMimeType);
}

QString McpProtocolResourceTemplate::name() const
{
    return mName;
}

void McpProtocolResourceTemplate::setName(const QString &newName)
{
    mName = newName;
}

std::optional<QString> McpProtocolResourceTemplate::title() const
{
    return mTitle;
}

void McpProtocolResourceTemplate::setTitle(std::optional<QString> newTitle)
{
    mTitle = std::move(newTitle);
}

QString McpProtocolResourceTemplate::uriTemplate() const
{
    return mUriTemplate;
}

void McpProtocolResourceTemplate::setUriTemplate(const QString &newUriTemplate)
{
    mUriTemplate = newUriTemplate;
}
