
#include "wrap-string.hpp"
#include "wrap-map.hpp"
#include "wrap-deque.hpp"
#include "wrap-sstream.hpp"

#include "invocation.hpp"

Invocation::Invocation() : kind_(AccessKind::INVALID), actor_(0), place_(0) {}

Invocation::Invocation(AccessKind kind, int64_t actor, int64_t place, std::string_view datapack) 
    : kind_(kind), actor_(actor), place_(place), datapack_(datapack) {}


void Invocation::serialize(StreamBuffer *sb) const {
    sb->write_uint8(int64_t(kind_));
    sb->write_int64(actor_);
    sb->write_int64(place_);
    sb->write_string(datapack_);
}

void Invocation::deserialize(StreamBuffer *sb) {
    kind_ = AccessKind(sb->read_uint8());
    actor_ = sb->read_int64();
    place_ = sb->read_int64();
    datapack_ = sb->read_string();
}

eng::string Invocation::debug_string() const {
    eng::ostringstream oss;
    oss << "inv[";
    switch (kind_) {
    case AccessKind::INVALID: oss << "invalid"; break;
    case AccessKind::INVOKE_LUA_CALL: oss << "lua_invoke"; break;
    case AccessKind::INVOKE_LUA_EXPR: oss << "lua_expr"; break;
    case AccessKind::INVOKE_FLUSH_PRINTS: oss << "flush_prints"; break;
    case AccessKind::INVOKE_TICK: oss << "tick"; break;
    case AccessKind::INVOKE_LUA_SOURCE: oss << "lua_source"; break;
    case AccessKind::PROBE_LUA_CALL: oss << "lua_probe"; break;
    default: oss << "UNKNOWN"; break;
    }
    oss << " a=" << actor_;
    oss << " p=" << place_;
    oss << " dp=" << datapack_.size() << " bytes";
    oss << "]";
    return oss.str();
}
