/*
  Copyright <2018-2025> <scott.e.graves@protonmail.com>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/
#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_
