#ifndef LUA_PERSISTENCE_H
#define LUA_PERSISTENCE_H

#include <string>
#include <vector>

extern "C" {
struct lua_State;
}

namespace wherigo {

class LuaPersistence {
public:
  // Save current Lua state to file
  static bool saveState(lua_State* L, const std::string& filePath);

  // Load Lua state from file
  static bool loadState(lua_State* L, const std::string& filePath);

  // Save specific global variables
  static bool saveGlobals(lua_State* L, const std::string& filePath,
                          const std::vector<std::string>& globals);

  // Load specific global variables
  static bool loadGlobals(lua_State* L, const std::string& filePath);

private:
  // Serialize a Lua value (recursive)
  static void serializeValue(lua_State* L, int index, std::string& output, int depth = 0);

  // Deserialize from string back to Lua stack
  static bool deserializeValue(lua_State* L, const std::string& input, size_t& pos);

  // Helper to serialize table
  static void serializeTable(lua_State* L, int index, std::string& output, int depth);

  // Collect all global variables that should be saved
  static std::vector<std::string> getGameGlobals(lua_State* L);
};

} // namespace wherigo

#endif // LUA_PERSISTENCE_H

