//------------------------------------------------------------------------------
/*
    This file is part of clio: https://github.com/XRPLF/clio
    Copyright (c) 2023, the clio developers.

    Permission to use, copy, modify, and distribute this software for any
    purpose with or without fee is hereby granted, provided that the above
    copyright notice and this permission notice appear in all copies.

    THE  SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    WITH  REGARD  TO  THIS  SOFTWARE  INCLUDING  ALL  IMPLIED  WARRANTIES  OF
    MERCHANTABILITY  AND  FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    ANY  SPECIAL,  DIRECT,  INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    WHATSOEVER  RESULTING  FROM  LOSS  OF USE, DATA OR PROFITS, WHETHER IN AN
    ACTION  OF  CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#pragma once

#include "data/BackendInterface.hpp"
#include "data/DBHelpers.hpp"
#include "etl/ETLServiceInterface.hpp"
#include "etl/LoadBalancerInterface.hpp"
#include "feed/SubscriptionManagerInterface.hpp"
#include "rpc/Errors.hpp"
#include "rpc/JS.hpp"
#include "rpc/common/Specs.hpp"
#include "rpc/common/Types.hpp"
#include "util/Assert.hpp"
#include "util/build/Build.hpp"

#include <boost/json/conversion.hpp>
#include <boost/json/object.hpp>
#include <boost/json/value.hpp>
#include <fmt/format.h>
#include <xrpl/basics/chrono.h>
#include <xrpl/basics/strHex.h>
#include <xrpl/protocol/BuildInfo.h>
#include <xrpl/protocol/ErrorCodes.h>
#include <xrpl/protocol/Fees.h>
#include <xrpl/protocol/Protocol.h>
#include <xrpl/protocol/jss.h>

#include <chrono>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <optional>
#include <string>

namespace rpc {
class Counters;
}  // namespace rpc

namespace rpc {

/**
 * @brief Contains common functionality for handling the `server_info` command
 *
 * @tparam CountersType The type of the counters
 */
template <typename CountersType>
class BaseServerInfoHandler {
    static constexpr auto kBACKEND_COUNTERS_KEY = "backend_counters";

    std::shared_ptr<BackendInterface> backend_;
    std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions_;
    std::shared_ptr<etl::LoadBalancerInterface> balancer_;
    std::shared_ptr<etl::ETLServiceInterface const> etl_;
    std::reference_wrapper<CountersType const> counters_;

public:
    /**
     * @brief A struct to hold the input data for the command
     */
    struct Input {
        bool backendCounters = false;
    };

    /**
     * @brief A struct to hold the admin section of the output
     */
    struct AdminSection {
        boost::json::object counters;
        std::optional<boost::json::object> backendCounters;
        boost::json::object subscriptions;
        boost::json::object etl;
    };

    /**
     * @brief A struct to hold the validated ledger section of the output
     */
    struct ValidatedLedgerSection {
        uint32_t age = 0;
        std::string hash;
        ripple::LedgerIndex seq = {};
        std::optional<ripple::Fees> fees = std::nullopt;
    };

    /**
     * @brief A struct to hold the cache section of the output
     */
    struct CacheSection {
        std::size_t size = 0;
        bool isEnabled = false;
        bool isFull = false;
        ripple::LedgerIndex latestLedgerSeq = {};
        float objectHitRate = 1.0;
        float successorHitRate = 1.0;
    };

    /**
     * @brief A struct to hold the info section of the output
     */
    struct InfoSection {
        std::optional<AdminSection> adminSection = std::nullopt;
        std::string completeLedgers;
        uint32_t loadFactor = 1u;
        std::chrono::time_point<std::chrono::system_clock> time = std::chrono::system_clock::now();
        std::chrono::seconds uptime = {};
        std::string clioVersion = util::build::getClioVersionString();
        std::string xrplVersion = ripple::BuildInfo::getVersionString();
        std::optional<boost::json::object> rippledInfo = std::nullopt;
        ValidatedLedgerSection validatedLedger = {};
        CacheSection cache = {};
        bool isAmendmentBlocked = false;
        bool isCorruptionDetected = false;
    };

    /**
     * @brief A struct to hold the output data of the command
     */
    struct Output {
        InfoSection info = {};

        // validated should be sent via framework
        bool validated = true;
    };

    using Result = HandlerReturnType<Output>;

    /**
     * @brief Construct a new BaseServerInfoHandler object
     *
     * @param backend The backend to use
     * @param subscriptions The subscription manager to use
     * @param balancer The load balancer to use
     * @param etl The ETL service to use
     * @param counters The counters to use
     */
    BaseServerInfoHandler(
        std::shared_ptr<BackendInterface> const& backend,
        std::shared_ptr<feed::SubscriptionManagerInterface> const& subscriptions,
        std::shared_ptr<etl::LoadBalancerInterface> const& balancer,
        std::shared_ptr<etl::ETLServiceInterface const> const& etl,
        CountersType const& counters
    )
        : backend_(backend)
        , subscriptions_(subscriptions)
        , balancer_(balancer)
        , etl_(etl)
        , counters_(std::cref(counters))
    {
    }

    /**
     * @brief Returns the API specification for the command
     *
     * @param apiVersion The api version to return the spec for
     * @return The spec for the given apiVersion
     */
    static RpcSpecConstRef
    spec([[maybe_unused]] uint32_t apiVersion)
    {
        static RpcSpec const kRPC_SPEC = {};
        return kRPC_SPEC;
    }

    /**
     * @brief Process the ServerInfo command
     *
     * @param input The input data for the command
     * @param ctx The context of the request
     * @return The result of the operation
     */
    Result
    process(Input const& input, Context const& ctx) const
    {
        using namespace rpc;
        using namespace std::chrono;

        auto const range = backend_->fetchLedgerRange();
        ASSERT(range.has_value(), "ServerInfo's ledger range must be available");

        auto const lgrInfo = backend_->fetchLedgerBySequence(range->maxSequence, ctx.yield);
        if (not lgrInfo.has_value())
            return Error{Status{RippledError::rpcINTERNAL}};

        auto const fees = backend_->fetchFees(lgrInfo->seq, ctx.yield);
        if (not fees.has_value())
            return Error{Status{RippledError::rpcINTERNAL}};

        auto output = Output{};
        auto const sinceEpoch = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
        auto const age = static_cast<int32_t>(sinceEpoch) -
            static_cast<int32_t>(lgrInfo->closeTime.time_since_epoch().count()) -
            static_cast<int32_t>(kRIPPLE_EPOCH_START);

        output.info.completeLedgers = fmt::format("{}-{}", range->minSequence, range->maxSequence);

        if (ctx.isAdmin) {
            output.info.adminSection = {
                .counters = counters_.get().report(),
                .backendCounters = input.backendCounters ? std::make_optional(backend_->stats()) : std::nullopt,
                .subscriptions = subscriptions_->report(),
                .etl = etl_->getInfo()
            };
        }

        auto const serverInfoRippled =
            balancer_->forwardToRippled({{"command", "server_info"}}, ctx.clientIp, ctx.isAdmin, ctx.yield);

        if (serverInfoRippled && !serverInfoRippled->contains(JS(error))) {
            if (serverInfoRippled->contains(JS(result)) &&
                serverInfoRippled->at(JS(result)).as_object().contains(JS(info))) {
                output.info.rippledInfo = serverInfoRippled->at(JS(result)).as_object().at(JS(info)).as_object();
            }
        }

        output.info.validatedLedger.age = age < 0 ? 0 : age;
        output.info.validatedLedger.hash = ripple::strHex(lgrInfo->hash);
        output.info.validatedLedger.seq = lgrInfo->seq;
        output.info.validatedLedger.fees = fees;
        output.info.cache.size = backend_->cache().size();
        output.info.cache.isFull = backend_->cache().isFull();
        output.info.cache.latestLedgerSeq = backend_->cache().latestLedgerSequence();
        output.info.cache.objectHitRate = backend_->cache().getObjectHitRate();
        output.info.cache.successorHitRate = backend_->cache().getSuccessorHitRate();
        output.info.cache.isEnabled = not backend_->cache().isDisabled();
        output.info.uptime = counters_.get().uptime();
        output.info.isAmendmentBlocked = etl_->isAmendmentBlocked();
        output.info.isCorruptionDetected = etl_->isCorruptionDetected();

        return output;
    }

private:
    friend void
    tag_invoke(boost::json::value_from_tag, boost::json::value& jv, Output const& output)
    {
        using boost::json::value_from;

        jv = {
            {JS(info), value_from(output.info)},
            {JS(validated), output.validated},
        };
    }

    friend void
    tag_invoke(boost::json::value_from_tag, boost::json::value& jv, InfoSection const& info)
    {
        using boost::json::value_from;
        using ripple::to_string;

        jv = {
            {JS(complete_ledgers), info.completeLedgers},
            {JS(load_factor), info.loadFactor},
            {JS(time), to_string(std::chrono::floor<std::chrono::microseconds>(info.time))},
            {JS(uptime), info.uptime.count()},
            {"clio_version", info.clioVersion},
            {"libxrpl_version", info.xrplVersion},
            {JS(validated_ledger), value_from(info.validatedLedger)},
            {"cache", value_from(info.cache)},
        };

        if (info.isAmendmentBlocked)
            jv.as_object()[JS(amendment_blocked)] = true;

        if (info.isCorruptionDetected)
            jv.as_object()["corruption_detected"] = true;

        if (info.rippledInfo) {
            auto const& rippledInfo = info.rippledInfo.value();

            if (rippledInfo.contains(JS(load_factor)))
                jv.as_object()[JS(load_factor)] = rippledInfo.at(JS(load_factor));
            if (rippledInfo.contains(JS(validation_quorum)))
                jv.as_object()[JS(validation_quorum)] = rippledInfo.at(JS(validation_quorum));
            if (rippledInfo.contains(JS(build_version)))
                jv.as_object()["rippled_version"] = rippledInfo.at(JS(build_version));
            if (rippledInfo.contains(JS(network_id)))
                jv.as_object()[JS(network_id)] = rippledInfo.at(JS(network_id));
        }

        if (info.adminSection) {
            jv.as_object()["etl"] = info.adminSection->etl;
            jv.as_object()[JS(counters)] = info.adminSection->counters;
            jv.as_object()[JS(counters)].as_object()["subscriptions"] = info.adminSection->subscriptions;
            if (info.adminSection->backendCounters.has_value()) {
                jv.as_object()[kBACKEND_COUNTERS_KEY] = *info.adminSection->backendCounters;
            }
        }
    }

    friend void
    tag_invoke(boost::json::value_from_tag, boost::json::value& jv, ValidatedLedgerSection const& validated)
    {
        jv = {
            {JS(age), validated.age},
            {JS(hash), validated.hash},
            {JS(seq), validated.seq},
            {JS(base_fee_xrp), validated.fees->base.decimalXRP()},
            {JS(reserve_base_xrp), validated.fees->reserve.decimalXRP()},
            {JS(reserve_inc_xrp), validated.fees->increment.decimalXRP()},
        };
    }

    friend void
    tag_invoke(boost::json::value_from_tag, boost::json::value& jv, CacheSection const& cache)
    {
        jv = {
            {"size", cache.size},
            {"is_enabled", cache.isEnabled},
            {"is_full", cache.isFull},
            {"latest_ledger_seq", cache.latestLedgerSeq},
            {"object_hit_rate", cache.objectHitRate},
            {"successor_hit_rate", cache.successorHitRate},
        };
    }

    friend Input
    tag_invoke(boost::json::value_to_tag<Input>, boost::json::value const& jv)
    {
        auto input = BaseServerInfoHandler::Input{};
        auto const jsonObject = jv.as_object();
        if (jsonObject.contains(kBACKEND_COUNTERS_KEY) && jsonObject.at(kBACKEND_COUNTERS_KEY).is_bool())
            input.backendCounters = jv.at(kBACKEND_COUNTERS_KEY).as_bool();
        return input;
    }
};

/**
 * @brief The server_info command asks the Clio server for a human-readable version of various information about the
 * Clio server being queried.
 *
 * For more details see: https://xrpl.org/server_info-clio.html
 */
using ServerInfoHandler = BaseServerInfoHandler<Counters>;

}  // namespace rpc
