/*
 * SPDX-FileCopyrightText: 2026 Oak Ridge National Laboratory and Contributors
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#ifndef ADIOS2_HELPER_ADIOSREROUTING_H_
#define ADIOS2_HELPER_ADIOSREROUTING_H_

#include "adios2/helper/adiosComm.h"

/// \cond EXCLUDE_FROM_DOXYGEN
#include <cstddef>
#include <cstdint>
#include <vector>
/// \endcond

namespace adios2
{

namespace helper
{

class RerouteMessage
{
public:
    enum class MessageType
    {
        DO_WRITE,
        WRITER_CAPACITY,
        WRITER_IDLE,
        WRITE_MORE,
        WRITE_COMPLETION,
        REROUTE_REQUEST,
        REROUTE_ACK,
        REROUTE_REJECT,
        GROUP_CLOSE,
        GROUP_CLOSE_ACK,
        STATUS_INQUIRY,
        STATUS_REPLY,
    };

    std::string GetTypeString(MessageType mtype)
    {
        switch (mtype)
        {
        case MessageType::DO_WRITE:
            return std::string("DO_WRITE");
            break;
        case MessageType::WRITER_IDLE:
            return std::string("WRITER_IDLE");
            break;
        case MessageType::WRITE_MORE:
            return std::string("WRITE_MORE");
            break;
        case MessageType::WRITE_COMPLETION:
            return std::string("WRITE_COMPLETION");
            break;
        default:
            return std::string("UNKNOWN");
            break;
        }
    }

    // Send the contents of this message to another rank
    void NonBlockingSendTo(helper::Comm &comm, int destRank, std::vector<char> &buffer);
    void BlockingSendTo(helper::Comm &comm, int destRank, std::vector<char> &buffer);

    // Receive a message from another rank to populate this message
    void BlockingRecvFrom(helper::Comm &comm, int srcRank, std::vector<char> &buffer);

    MessageType m_MsgType;
    int m_SrcRank;
    int m_DestRank;
    int m_WildCard;
    uint64_t m_Offset;
    uint64_t m_Size;

    static const size_t REROUTE_MESSAGE_SIZE = sizeof(MessageType) + sizeof(int) + sizeof(int) +
                                               sizeof(int) + sizeof(uint64_t) + sizeof(uint64_t);
};

} // end namespace helper
} // end namespace adios2

#endif /* ADIOS2_HELPER_ADIOSREROUTING_H_ */
