#pragma once

#include <httplib.h>

#include "mission/mission_queue.hpp"
#include "mission/mission_scheduler.hpp"
#include "mission/mission_store.hpp"
#include "mission/modbus_trigger_service.hpp"
#include "robot/robot_runtime.hpp"
#include "storage/dashboard_store.hpp"
#include "storage/map_store.hpp"
#include "storage/site_store.hpp"
#include "storage/sound_store.hpp"
#include "storage/io_module_store.hpp"
#include "io/io_module_service.hpp"
#include "storage/path_store.hpp"
#include "storage/path_guide_store.hpp"
#include "path/path_service.hpp"
#include "storage/transition_store.hpp"
#include "storage/state_repository.hpp"

namespace lm {

class ApiServer
{
public:
  ApiServer(StateRepository& repo,
            MissionQueue& mission_queue,
            MissionStore& mission_store,
            ModbusTriggerService& modbus,
            MissionScheduler& scheduler,
            RobotRuntime& robot_runtime,
            MapStore& map_store,
            SiteStore& site_store,
            SoundStore& sound_store,
            TransitionStore& transition_store,
            IoModuleStore& io_module_store,
            IoModuleService& io_module_service,
            PathStore& path_store,
            PathGuideStore& path_guide_store,
            PathService& path_service,
            DashboardStore& dashboard_store);

  void registerRoutes(httplib::Server& svr);

private:
  StateRepository& repo_;
  MissionQueue& mission_queue_;
  MissionStore& mission_store_;
  ModbusTriggerService& modbus_;
  MissionScheduler& scheduler_;
  RobotRuntime& robot_runtime_;
  MapStore& map_store_;
  SiteStore& site_store_;
  SoundStore& sound_store_;
  TransitionStore& transition_store_;
  IoModuleStore& io_module_store_;
  IoModuleService& io_module_service_;
  PathStore& path_store_;
  PathGuideStore& path_guide_store_;
  PathService& path_service_;
  DashboardStore& dashboard_store_;

  bool enqueueRequest(const nlohmann::json& request, httplib::Response& res, int status_code = 201);
  std::optional<nlohmann::json> enqueueMission(const nlohmann::json& request, std::string& err);
  nlohmann::json toMirQueueEntry(const nlohmann::json& entry) const;
  void registerMissionRoutes(httplib::Server& svr);
  void registerMirV2Routes(httplib::Server& svr);
  void registerIntegrationRoutes(httplib::Server& svr);
  void registerRobotRoutes(httplib::Server& svr);
  void registerMediaRoutes(httplib::Server& svr);
  void registerTransitionRoutes(httplib::Server& svr);
  void registerIoModuleRoutes(httplib::Server& svr);
  void registerPathRoutes(httplib::Server& svr);
  void registerPathGuideRoutes(httplib::Server& svr);
  void registerDashboardRoutes(httplib::Server& svr);
};

}  // namespace lm
