/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

#include "iceberg/catalog/rest/catalog_properties.h"

#include <algorithm>
#include <string>
#include <string_view>

namespace iceberg::rest {

RestCatalogProperties RestCatalogProperties::default_properties() {
  return RestCatalogProperties();
}

RestCatalogProperties RestCatalogProperties::FromMap(
    std::unordered_map<std::string, std::string> properties) {
  RestCatalogProperties rest_catalog_config;
  rest_catalog_config.configs_ = std::move(properties);
  return rest_catalog_config;
}

std::unordered_map<std::string, std::string> RestCatalogProperties::ExtractHeaders()
    const {
  return Extract(kHeaderPrefix);
}

Result<std::string_view> RestCatalogProperties::Uri() const {
  auto it = configs_.find(kUri.key());
  if (it == configs_.end() || it->second.empty()) {
    return InvalidArgument("Rest catalog configuration property 'uri' is required.");
  }
  return it->second;
}

Result<SnapshotMode> RestCatalogProperties::SnapshotLoadingMode() const {
  std::string upper = StringUtils::ToUpper(Get(kSnapshotLoadingMode));
  if (upper == "ALL") {
    return SnapshotMode::kAll;
  } else if (upper == "REFS") {
    return SnapshotMode::kRefs;
  } else {
    return InvalidArgument("Invalid snapshot loading mode: '{}'.",
                           Get(kSnapshotLoadingMode));
  }
}

}  // namespace iceberg::rest
