/**
 * Copyright 2011-2024 Centreon
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * For more information : contact@centreon.com
 */

#include "com/centreon/broker/graphite/factory.hh"
#include <absl/strings/match.h>
#include "com/centreon/broker/cache/global_cache.hh"
#include "com/centreon/broker/config/applier/state.hh"
#include "com/centreon/broker/graphite/connector.hh"
#include "com/centreon/common/pool.hh"
#include "com/centreon/exceptions/msg_fmt.hh"

using namespace com::centreon::broker;
using namespace com::centreon::broker::graphite;
using namespace com::centreon::exceptions;

/**
 *  Find a parameter in configuration.
 *
 *  @param[in] cfg Configuration object.
 *  @param[in] key Property to get.
 *
 *  @return Property value.
 */
static std::string find_param(config::endpoint const& cfg,
                              std::string const& key) {
  auto it = cfg.params.find(key);
  if (cfg.params.end() == it)
    throw msg_fmt("graphite: no '{}' defined for endpoint '{}'", key, cfg.name);
  return it->second;
}

/**
 *  Get a parameter in configuration, or return a default value.
 *
 *  @param[in] cfg Configuration object.
 *  @param[in] key Property to get.
 *  @param[in] def The default value if nothing found.
 *
 *  @return Property value.
 */
static std::string get_string_param(config::endpoint const& cfg,
                                    std::string const& key,
                                    std::string const& def) {
  auto it = cfg.params.find(key);
  if (cfg.params.end() == it)
    return def;
  else
    return it->second;
}

/**
 *  Get a parameter in configuration, or return a default value.
 *
 *  @param[in] cfg Configuration object.
 *  @param[in] key Property to get.
 *  @param[in] def The default value if nothing found.
 *
 *  @return Property value.
 */
static uint32_t get_uint_param(config::endpoint const& cfg,
                               std::string const& key,
                               uint32_t def) {
  auto it = cfg.params.find(key);
  uint32_t retval = 0;
  if (cfg.params.end() == it)
    return def;
  else {
    if (!absl::SimpleAtoi(it->second, &retval)) {
      throw msg_fmt("graphite: '{}' must be numeric for endpoint '{}'", key,
                    cfg.name);
    }
  }

  return retval;
}

/**
 *  Check if a configuration match the storage layer.
 *
 *  @param[in] cfg  Endpoint configuration.
 *
 *  @return true if the configuration matches the storage layer.
 */
bool factory::has_endpoint(const config::endpoint& cfg,
                           io::extension* ext) const {
  if (ext)
    *ext = io::extension("GRAPHITE", false, false);
  return absl::EqualsIgnoreCase(cfg.type, "graphite");
}

/**
 * @brief Set the default values to the endpoint config read from cfg files
 *
 * @param cfg config to update
 */
void factory::set_default_values(config::endpoint& cfg) const {
  cfg.params["cache"] = "no";
  cfg.cache_enabled = false;
}

/**
 *  Build a storage endpoint from a configuration.
 *
 *  @param[in]  cfg         Endpoint configuration.
 *  @param[out] is_acceptor Will be set to false.
 *  @param[in]  cache       The persistent cache.
 *
 *  @return Endpoint matching the given configuration.
 */
io::endpoint* factory::new_endpoint(
    config::endpoint& cfg,
    const std::map<std::string, std::string>& global_params [[maybe_unused]],
    bool& is_acceptor,
    std::shared_ptr<persistent_cache>) const {
  std::string db_host(find_param(cfg, "db_host"));
  unsigned short db_port(get_uint_param(cfg, "db_port", 2003));
  std::string db_user(get_string_param(cfg, "db_user", ""));
  std::string db_password(get_string_param(cfg, "db_password", ""));
  uint32_t queries_per_transaction(
      get_uint_param(cfg, "queries_per_transaction", 1));
  std::string metric_naming(
      get_string_param(cfg, "metric_naming", "centreon.metrics.$METRICID$"));
  std::string status_naming(
      get_string_param(cfg, "status_naming", "centreon.statuses.$INDEXID$"));
  std::string escape_string(get_string_param(cfg, "escape_string", "_"));

  if (config::applier::state::loaded()) {  // false only happens in UTs
    cache::global_cache::load(
        com::centreon::common::pool::io_context_ptr(),
        config::applier::state::instance().cache_dir() + ".cache.global");
  }

  // Connector.
  std::unique_ptr<graphite::connector> c(new graphite::connector);
  c->connect_to(metric_naming, status_naming, escape_string, db_user,
                db_password, db_host, db_port, queries_per_transaction);
  is_acceptor = false;
  return (c.release());
}
