//------------------------------------------------------------------------------
/*
    This file is part of clio: https://github.com/XRPLF/clio
    Copyright (c) 2024, 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/common/Types.hpp"
#include "rpc/handlers/AMMInfo.hpp"
#include "rpc/handlers/AccountChannels.hpp"
#include "rpc/handlers/AccountCurrencies.hpp"
#include "rpc/handlers/AccountInfo.hpp"
#include "rpc/handlers/AccountLines.hpp"
#include "rpc/handlers/AccountMPTokenIssuances.hpp"
#include "rpc/handlers/AccountMPTokens.hpp"
#include "rpc/handlers/AccountNFTs.hpp"
#include "rpc/handlers/AccountObjects.hpp"
#include "rpc/handlers/AccountOffers.hpp"
#include "rpc/handlers/AccountTx.hpp"
#include "rpc/handlers/BookChanges.hpp"
#include "rpc/handlers/BookOffers.hpp"
#include "rpc/handlers/DepositAuthorized.hpp"
#include "rpc/handlers/Feature.hpp"
#include "rpc/handlers/GatewayBalances.hpp"
#include "rpc/handlers/GetAggregatePrice.hpp"
#include "rpc/handlers/Ledger.hpp"
#include "rpc/handlers/LedgerData.hpp"
#include "rpc/handlers/LedgerEntry.hpp"
#include "rpc/handlers/LedgerIndex.hpp"
#include "rpc/handlers/MPTHolders.hpp"
#include "rpc/handlers/NFTBuyOffers.hpp"
#include "rpc/handlers/NFTHistory.hpp"
#include "rpc/handlers/NFTInfo.hpp"
#include "rpc/handlers/NFTSellOffers.hpp"
#include "rpc/handlers/NFTsByIssuer.hpp"
#include "rpc/handlers/NoRippleCheck.hpp"
#include "rpc/handlers/ServerInfo.hpp"
#include "rpc/handlers/Subscribe.hpp"
#include "rpc/handlers/TransactionEntry.hpp"
#include "rpc/handlers/VaultInfo.hpp"
#include "util/Assert.hpp"
#include "util/HandlerBaseTestFixture.hpp"
#include "util/MockAmendmentCenter.hpp"
#include "util/MockAssert.hpp"
#include "util/MockCounters.hpp"
#include "util/MockCountersFixture.hpp"
#include "util/MockETLServiceTestFixture.hpp"
#include "util/MockSubscriptionManager.hpp"
#include "util/MockWsBase.hpp"
#include "util/TestObject.hpp"
#include "web/SubscriptionContextInterface.hpp"

#include <boost/asio/spawn.hpp>
#include <gtest/gtest.h>
#include <xrpl/protocol/AccountID.h>
#include <xrpl/protocol/Book.h>
#include <xrpl/protocol/UintTypes.h>

#include <memory>
#include <string>
#include <vector>

using ::testing::Types;
using namespace rpc;
using TestServerInfoHandler = BaseServerInfoHandler<MockCounters>;

static constexpr auto kINDEX1 = "05FB0EB4B899F056FA095537C5817163801F544BAFCEA39C995D76DB4D16F9DD";
static constexpr auto kAMM_ACCOUNT = "rLcS7XL6nxRAi7JcbJcn1Na179oF3vdfbh";
static constexpr auto kACCOUNT = "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn";
static constexpr auto kNFT_ID = "00010000A7CAD27B688D14BA1A9FA5366554D6ADCF9CE0875B974D9F00000004";
static constexpr auto kCURRENCY = "0158415500000000C1F76FF6ECB0BAC600000000";
static constexpr auto kVAULT_ID = "61B03A6F8CEBD3AF9D8F696C3D0A9A9F0493B34BF6B5D93CF0BC009E6BA75303";

using AnyHandlerType = Types<
    AccountChannelsHandler,
    AccountCurrenciesHandler,
    AccountInfoHandler,
    AccountLinesHandler,
    AccountMPTokenIssuancesHandler,
    AccountMPTokensHandler,
    AccountNFTsHandler,
    AccountObjectsHandler,
    AccountOffersHandler,
    AccountTxHandler,
    AMMInfoHandler,
    BookChangesHandler,
    BookOffersHandler,
    DepositAuthorizedHandler,
    FeatureHandler,
    GatewayBalancesHandler,
    GetAggregatePriceHandler,
    LedgerHandler,
    LedgerDataHandler,
    LedgerEntryHandler,
    LedgerIndexHandler,
    MPTHoldersHandler,
    NFTsByIssuerHandler,
    NFTHistoryHandler,
    NFTBuyOffersHandler,
    NFTInfoHandler,
    NFTSellOffersHandler,
    NoRippleCheckHandler,
    TestServerInfoHandler,
    SubscribeHandler,
    TransactionEntryHandler,
    VaultInfoHandler>;

template <typename HandlerType>
struct AllHandlersAssertTest : common::util::WithMockAssert,
                               HandlerBaseTest,
                               MockLoadBalancerTest,
                               MockCountersTest,
                               testing::WithParamInterface<std::string> {
    AllHandlersAssertTest() : handler_{initHandler()}
    {
        ASSERT(mockAmendmentCenterPtr_.amendmentCenterMock != nullptr, "mockAmendmentCenterPtr is not initialized.");
        ASSERT(
            mockSubscriptionManagerPtr_.subscriptionManagerMock != nullptr, "mockSubscriptionPtr is not initialized"
        );
    }

protected:
    web::SubscriptionContextPtr session_ = std::make_shared<MockSession>();
    MockSession* mockSession_ = dynamic_cast<MockSession*>(session_.get());
    StrictMockSubscriptionManagerSharedPtr mockSubscriptionManagerPtr_;
    StrictMockAmendmentCenterSharedPtr mockAmendmentCenterPtr_;
    HandlerType handler_;

private:
    HandlerType
    initHandler()
    {
        if constexpr (std::is_same_v<HandlerType, AccountInfoHandler> || std::is_same_v<HandlerType, AMMInfoHandler> ||
                      std::is_same_v<HandlerType, LedgerHandler> || std::is_same_v<HandlerType, BookOffersHandler> ||
                      std::is_same_v<HandlerType, FeatureHandler>) {
            return HandlerType{this->backend_, this->mockAmendmentCenterPtr_};
        } else if constexpr (std::is_same_v<HandlerType, SubscribeHandler>) {
            return HandlerType{this->backend_, this->mockAmendmentCenterPtr_, this->mockSubscriptionManagerPtr_};
        } else if constexpr (std::is_same_v<HandlerType, AccountTxHandler>) {
            return HandlerType{this->backend_, mockETLServicePtr_};
        } else if constexpr (std::is_same_v<HandlerType, TestServerInfoHandler>) {
            return HandlerType{
                this->backend_,
                this->mockSubscriptionManagerPtr_,
                mockLoadBalancerPtr_,
                mockETLServicePtr_,
                *mockCountersPtr_
            };
        } else {
            return HandlerType{this->backend_};
        }
    }
};

template <typename Handler>
static Handler::Input
createInput()
{
    return typename Handler::Input{};
}

// need to set specific values for input for some handler's to pass checks in .process() function
template <>
AccountInfoHandler::Input
createInput<AccountInfoHandler>()
{
    AccountInfoHandler::Input input{};
    input.account = kACCOUNT;
    input.ident = "asdf";
    return input;
}

template <>
AccountTxHandler::Input
createInput<AccountTxHandler>()
{
    AccountTxHandler::Input input{};
    input.account = kACCOUNT;
    return input;
}

template <>
AMMInfoHandler::Input
createInput<AMMInfoHandler>()
{
    AMMInfoHandler::Input input{};
    input.ammAccount = getAccountIdWithString(kAMM_ACCOUNT);
    return input;
}

template <>
BookOffersHandler::Input
createInput<BookOffersHandler>()
{
    BookOffersHandler::Input input{};
    input.paysCurrency = ripple::xrpCurrency();
    input.getsCurrency = ripple::Currency(kCURRENCY);
    input.paysID = ripple::xrpAccount();
    input.getsID = getAccountIdWithString(kACCOUNT);

    return input;
}

template <>
LedgerEntryHandler::Input
createInput<LedgerEntryHandler>()
{
    LedgerEntryHandler::Input input{};
    input.index = kINDEX1;
    return input;
}

template <>
NFTBuyOffersHandler::Input
createInput<NFTBuyOffersHandler>()
{
    NFTBuyOffersHandler::Input input{};
    input.nftID = kNFT_ID;
    return input;
}

template <>
NFTInfoHandler::Input
createInput<NFTInfoHandler>()
{
    NFTInfoHandler::Input input{};
    input.nftID = kNFT_ID;
    return input;
}

template <>
NFTSellOffersHandler::Input
createInput<NFTSellOffersHandler>()
{
    NFTSellOffersHandler::Input input{};
    input.nftID = kNFT_ID;
    return input;
}

template <>
SubscribeHandler::Input
createInput<SubscribeHandler>()
{
    SubscribeHandler::Input input{};

    input.books = std::vector<SubscribeHandler::OrderBook>{
        SubscribeHandler::OrderBook{.book = ripple::Book{}, .taker = kACCOUNT, .snapshot = true, .both = true}
    };
    return input;
}

template <>
VaultInfoHandler::Input
createInput<VaultInfoHandler>()
{
    VaultInfoHandler::Input input{};
    input.vaultID = kVAULT_ID;

    return input;
}

TYPED_TEST_CASE(AllHandlersAssertTest, AnyHandlerType);

TYPED_TEST(AllHandlersAssertTest, NoRangeAvailable)
{
    // doesn't work without 'this'
    this->runSpawn(
        [&](boost::asio::yield_context yield) {
            TypeParam const handler = this->handler_;

            auto const input = createInput<TypeParam>();
            auto const context = Context{yield, this->session_};

            EXPECT_CLIO_ASSERT_FAIL_WITH_MESSAGE(
                { [[maybe_unused]] auto unused = handler.process(input, context); }, "Assertion .* failed at .*"
            );
        },
        true
    );
}
