// Copyright 2018 Bloomberg Finance L.P
//
// 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.

#ifndef INCLUDED_PROTOS
#define INCLUDED_PROTOS

#include <buildboxcommon_protos.h>

#include <google/longrunning/operations.grpc.pb.h>
#include <google/longrunning/operations.pb.h>
#include <google/rpc/code.pb.h>
#include <google/rpc/status.pb.h>

#include <set>
#include <sstream>

namespace recc {
namespace proto {
using namespace buildboxcommon; // REAPI and ByteStream protos
using namespace google::longrunning;

static const std::set<std::string> s_reapiSupportedVersions = {
    "2.1", "2.2", "2.3", "2.4",  "2.5",  "2.6",
    "2.7", "2.8", "2.9", "2.10", "2.11", "2.12"};

inline std::string reapiSupportedVersionsList()
{
    std::ostringstream res;

    auto it = s_reapiSupportedVersions.cbegin();
    while (it != s_reapiSupportedVersions.cend()) {
        res << '"' << *it++ << '"';
        if (it != s_reapiSupportedVersions.cend()) {
            res << ", ";
        }
    }

    return res.str();
}

} // namespace proto

/**
 * If the given Status isn't OK, throw an exception.
 */
inline void ensure_ok(const grpc::Status &status)
{
    if (!status.ok()) {
        throw std::runtime_error("GRPC error " +
                                 std::to_string(status.error_code()) + ": " +
                                 status.error_message());
    }
}
inline void ensure_ok(const google::rpc::Status &status)
{
    if (status.code() != google::rpc::Code::OK) {
        throw std::runtime_error(status.ShortDebugString());
    }
}

} // namespace recc

#endif
