#include "protocol/envelope.h"

#ifdef VOICECAT_HAS_NET

#include "protocol/protocol.h"

#include <atomic>
#include <string>

namespace voicecat::protocol {

bool encode_envelope(const voicecat::v1::Envelope& env, std::vector<uint8_t>& out) {
    std::string bytes;
    if (!env.SerializeToString(&bytes)) return false;
    FrameCodec::emit(reinterpret_cast<const uint8_t*>(bytes.data()), bytes.size(), out);
    return true;
}

bool decode_envelope(const uint8_t* data, size_t len, voicecat::v1::Envelope& out) {
    return out.ParseFromArray(data, static_cast<int>(len));
}

bool decode_envelope(const std::vector<uint8_t>& frame, voicecat::v1::Envelope& out) {
    return decode_envelope(frame.data(), frame.size(), out);
}

uint64_t next_request_id() {
    static std::atomic<uint64_t> counter{1};
    return counter.fetch_add(1, std::memory_order_relaxed);
}

}  // namespace voicecat::protocol

#endif  // VOICECAT_HAS_NET
