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

#include "rpc/handlers/NFTInfo.hpp"

#include "rpc/Errors.hpp"
#include "rpc/JS.hpp"
#include "rpc/RPCHelpers.hpp"
#include "rpc/common/Types.hpp"
#include "util/Assert.hpp"
#include "util/JsonUtils.hpp"

#include <boost/json/conversion.hpp>
#include <boost/json/object.hpp>
#include <boost/json/value.hpp>
#include <boost/json/value_to.hpp>
#include <xrpl/basics/base_uint.h>
#include <xrpl/basics/strHex.h>
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/LedgerHeader.h>
#include <xrpl/protocol/jss.h>
#include <xrpl/protocol/nft.h>

#include <string>

using namespace ripple;

namespace rpc {

NFTInfoHandler::Result
NFTInfoHandler::process(NFTInfoHandler::Input const& input, Context const& ctx) const
{
    auto const tokenID = ripple::uint256{input.nftID.c_str()};
    auto const range = sharedPtrBackend_->fetchLedgerRange();
    ASSERT(range.has_value(), "NFTInfo's ledger range must be available");

    auto const expectedLgrInfo = getLedgerHeaderFromHashOrSeq(
        *sharedPtrBackend_, ctx.yield, input.ledgerHash, input.ledgerIndex, range->maxSequence
    );

    if (!expectedLgrInfo.has_value())
        return Error{expectedLgrInfo.error()};

    auto const& lgrInfo = expectedLgrInfo.value();
    auto const maybeNft = sharedPtrBackend_->fetchNFT(tokenID, lgrInfo.seq, ctx.yield);

    if (not maybeNft.has_value())
        return Error{Status{RippledError::rpcOBJECT_NOT_FOUND, "NFT not found"}};

    // TODO - this formatting is exactly the same and SHOULD REMAIN THE SAME
    // for each element of the `nfts_by_issuer` API. We should factor this out
    // so that the formats don't diverge. In the mean time, do not make any
    // changes to this formatting without making the same changes to that
    // formatting.
    auto const& nft = *maybeNft;
    auto output = NFTInfoHandler::Output{};

    output.nftID = strHex(nft.tokenID);
    output.ledgerIndex = nft.ledgerSequence;
    output.owner = toBase58(nft.owner);
    output.isBurned = nft.isBurned;
    output.flags = nft::getFlags(nft.tokenID);
    output.transferFee = nft::getTransferFee(nft.tokenID);
    output.issuer = toBase58(nft::getIssuer(nft.tokenID));
    output.taxon = nft::toUInt32(nft::getTaxon(nft.tokenID));
    output.serial = nft::getSerial(nft.tokenID);
    output.uri = strHex(nft.uri);

    return output;
}

void
tag_invoke(boost::json::value_from_tag, boost::json::value& jv, NFTInfoHandler::Output const& output)
{
    // TODO: use JStrings when they become available
    jv = boost::json::object{
        {JS(nft_id), output.nftID},
        {JS(ledger_index), output.ledgerIndex},
        {JS(owner), output.owner},
        {JS(is_burned), output.isBurned},
        {JS(flags), output.flags},
        {"transfer_fee", output.transferFee},
        {JS(issuer), output.issuer},
        {JS(nft_taxon), output.taxon},
        {JS(nft_serial), output.serial},
        {JS(validated), output.validated},
        {JS(uri), output.uri},
    };
}

NFTInfoHandler::Input
tag_invoke(boost::json::value_to_tag<NFTInfoHandler::Input>, boost::json::value const& jv)
{
    auto const& jsonObject = jv.as_object();
    auto input = NFTInfoHandler::Input{};

    input.nftID = boost::json::value_to<std::string>(jsonObject.at(JS(nft_id)));

    if (jsonObject.contains(JS(ledger_hash)))
        input.ledgerHash = boost::json::value_to<std::string>(jsonObject.at(JS(ledger_hash)));

    if (jsonObject.contains(JS(ledger_index))) {
        auto const expectedLedgerIndex = util::getLedgerIndex(jsonObject.at(JS(ledger_index)));
        if (expectedLedgerIndex.has_value())
            input.ledgerIndex = *expectedLedgerIndex;
    }

    return input;
}

}  // namespace rpc
