#pragma once

#include <nlohmann/json.hpp>

#include <mutex>
#include <string>

namespace lm {

class Database;
class MissionQueue;

class RobotRuntime
{
public:
  explicit RobotRuntime(Database& db, MissionQueue& mission_queue);

  nlohmann::json status() const;
  bool start(std::string& err);
  bool pause(std::string& err);
  bool resetError(std::string& err);
  bool setCmdVel(double linear, double angular, std::string& err);
  bool setJoystick(bool engaged, const std::string& speed, std::string& err);
  bool setActiveMap(const std::string& map_id, std::string& err);
  void clearActiveMapIf(const std::string& map_id);
  bool queueSoundPlay(const std::string& sound_id, int volume, std::string& err);
  void tick();

private:
  Database& db_;
  MissionQueue& mission_queue_;
  mutable std::mutex mu_;
  nlohmann::json state_;

  void load();
  void saveUnlocked() const;
  void ensureDefaultsUnlocked();
  nlohmann::json buildStatusUnlocked() const;
};

}  // namespace lm
