/*
 * Copyright (C) 2026 UBports Foundation.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "RcloneItem.h"

#include "item_id.h"

#include <QDateTime>
#include <QString>
#include <lomiri/storage/common.h>

using namespace std;
using namespace lomiri::storage::provider;
using namespace lomiri::storage::metadata;
using lomiri::storage::ItemType;

namespace
{

void fill_common(Item& item, QJsonObject const& entry)
{
    item.etag = make_etag(entry);

    QDateTime modtime = QDateTime::fromString(
        entry.value(QStringLiteral("ModTime")).toString(),
        Qt::ISODateWithMs).toUTC();
    if (modtime.isValid())
        item.metadata[LAST_MODIFIED_TIME] =
            modtime.toString(Qt::ISODate).toStdString();

    bool is_dir = entry.value(QStringLiteral("IsDir")).toBool();
    if (!is_dir)
    {
        double size = entry.value(QStringLiteral("Size")).toDouble(-1);
        if (size >= 0)
            item.metadata[SIZE_IN_BYTES] = static_cast<int64_t>(size);

        QString mime = entry.value(QStringLiteral("MimeType")).toString();
        if (!mime.isEmpty())
            item.metadata[CONTENT_TYPE] = mime.toStdString();
    }
}

}  // namespace

// Return true if the item exists
bool stat_exists(QJsonObject const& result)
{
    return result.value(QStringLiteral("item")).isObject();
}

// Identify a version of an object by size and UTC modification time
// TODO: use hash when rclone remote isn't flagged SlowHash
string make_etag(QJsonObject const& entry)
{
    QDateTime modtime = QDateTime::fromString(
        entry.value(QStringLiteral("ModTime")).toString(),
        Qt::ISODateWithMs).toUTC();
    if (!modtime.isValid()) return string();

    int64_t size = entry.value(QStringLiteral("Size")).toDouble(-1);
    return to_string(size) + "," +
           modtime.toString(Qt::ISODateWithMs).toStdString();
}

// Build an Item from an rclone entry. By default id is the item's own id and
// its parent is derived from it (lookup); with infer_child, id is the parent
// and the item's id is derived from the entry's name instead (listing).
Item make_item(QJsonObject const& entry, string const& id, bool infer_child)
{
    Item item;
    string name = entry.value(QStringLiteral("Name")).toString().toStdString();
    bool is_dir = entry.value(QStringLiteral("IsDir")).toBool();

    if (infer_child)
    {
        item.item_id = make_child_id(id, name, is_dir);
        item.parent_ids = {id};
    }
    else
    {
        item.item_id = id;
        item.parent_ids = {parent_of(id)};
    }

    item.name = name;
    item.type = is_dir ? ItemType::folder : ItemType::file;
    fill_common(item, entry);
    return item;
}

Item make_root_item(QJsonObject const& item_json)
{
    Item item;
    item.item_id = ".";
    item.name = "Root";
    item.type = ItemType::root;
    fill_common(item, item_json);
    return item;
}
