/*
 * 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/>.
 */

#pragma once

#include "RcloneClient.h"

#include <lomiri/storage/provider/Item.h>

#include <boost/thread/future.hpp>

#include <functional>
#include <map>
#include <memory>
#include <string>

enum class ExistsError { Exists, Conflict };

// Stat item_id and run proceed() only if nothing is there, otherwise fail
void verify_not_exists(
    std::shared_ptr<RcloneClient> const& client,
    std::shared_ptr<boost::promise<lomiri::storage::provider::Item>> const& promise,
    std::string const& fs, std::string const& remote,
    std::string const& item_id, std::string const& name,
    std::function<void()> proceed,
    ExistsError on_exists = ExistsError::Exists);

// Stat item_id and fulfil the promise with the resulting Item
void fetch_item_metadata(
    std::shared_ptr<RcloneClient> const& client,
    std::shared_ptr<boost::promise<lomiri::storage::provider::Item>> const& promise,
    std::string const& fs, std::string const& remote,
    std::string const& item_id);

// Define an rclone remote, operations can use it as "<name>:"
void create_remote(
    std::shared_ptr<RcloneClient> const& client, std::string const& name,
    std::string const& type,
    std::map<std::string, std::string> const& params,
    std::function<void(boost::exception_ptr const& error)> cb);

// Stat item_id and run cb() with metadata if the etag matches or with an Exception
void verify_etag(
    std::shared_ptr<RcloneClient> const& client,
    std::string const& fs, std::string const& remote, std::string const& item_id,
    std::string const& expected_etag, RcloneClient::RcCallback cb);
