#ifndef LIBMONITARR_INCLUDE_SETTINGS_HPP_
#define LIBMONITARR_INCLUDE_SETTINGS_HPP_

#include "utils/config.hpp"

namespace monitarr {
inline constexpr const auto default_interval{
    std::chrono::minutes{5U},
};

inline constexpr const auto default_timeout{
    std::chrono::minutes{24U * 60U},
};

struct server_cfg final {
  std::string id;
  std::string api_key;
  std::string api_version;
  std::chrono::minutes timeout{default_timeout};
  std::string url;
};

struct app_config final {
  std::chrono::minutes check_interval{default_interval};
  std::vector<server_cfg> server_list;

  void load(std::string_view file_path);

  void save(std::string_view file_path) const;
};
} // namespace monitarr

NLOHMANN_JSON_NAMESPACE_BEGIN
template <> struct adl_serializer<monitarr::server_cfg> {
  static void to_json(json &data, const monitarr::server_cfg &value) {
    data["id"] = value.id;
    data["api_key"] = value.api_key;
    data["api_version"] = value.api_version;
    data["timeout_minutes"] = value.timeout.count();
    data["url"] = value.url;
  }

  static void from_json(const json &data, monitarr::server_cfg &value) {
    data.at("id").get_to(value.id);
    data.at("api_key").get_to(value.api_key);
    data.at("api_version").get_to(value.api_version);
    value.timeout =
        std::chrono::minutes(data.at("timeout_minutes").get<std::int32_t>());
    data.at("url").get_to(value.url);
  }
};

template <> struct adl_serializer<monitarr::app_config> {
  static void to_json(json &data, const monitarr::app_config &value) {
    data["check_interval_minutes"] = value.check_interval.count();
    data["server_list"] = value.server_list;
  }

  static void from_json(const json &data, monitarr::app_config &value) {
    value.check_interval = std::chrono::minutes(
        data.at("check_interval_minutes").get<std::int32_t>());
    data.at("server_list").get_to(value.server_list);
  }
};
NLOHMANN_JSON_NAMESPACE_END

#endif // LIBMONITARR_INCLUDE_SETTINGS_HPP_
