/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *   Copyright 2020-Present Couchbase, Inc.
 *
 *   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.
 */

#pragma once

#include <couchbase/error_context.hxx>

#include <cstdint>
#include <memory>

namespace couchbase
{
#ifndef COUCHBASE_CXX_CLIENT_DOXYGEN
class internal_search_error_context;
#endif

/**
 * The error context returned with Search operations.
 *
 * @since 1.0.0
 * @committed
 */
class search_error_context : public error_context
{
  public:
    search_error_context();
    explicit search_error_context(internal_search_error_context ctx);
    search_error_context(search_error_context&& other);
    search_error_context& operator=(search_error_context&& other);
    search_error_context(const search_error_context& other) = delete;
    search_error_context& operator=(const search_error_context& other) = delete;
    ~search_error_context() override;

    /**
     * Returns status of the operation.
     *
     * @see couchbase::errc error codes that might be generated by the library
     *
     * @return error code or false-like value for success
     *
     * @since 1.0.0
     * @committed
     */
    [[nodiscard]] auto ec() const -> std::error_code override;

    /**
     * The hostname/ip where this request got last dispatched to.
     *
     * @return address encoded as a string
     *
     * @since 1.0.0
     * @committed
     */
    [[nodiscard]] auto last_dispatched_to() const -> const std::optional<std::string>& override;

    /**
     * The hostname/ip where this request got last dispatched from.
     *
     * @return address encoded as a string
     *
     * @since 1.0.0
     * @committed
     */
    [[nodiscard]] auto last_dispatched_from() const -> const std::optional<std::string>& override;

    /**
     * The number of times the attached request has been retried.
     *
     * @return number of retries.
     *
     * @since 1.0.0
     * @committed
     */
    [[nodiscard]] auto retry_attempts() const -> std::size_t override;

    /**
     * Set of reasons recorded during retrying the operation.
     *
     * @return set of reasons (empty if the operation was not retried)
     *
     * @since 1.0.0
     * @committed
     */
    [[nodiscard]] auto retry_reasons() const -> const std::set<retry_reason>& override;

    /**
     * Check if the operation was retried because of given reason.
     *
     * @param reason reason to check
     * @return true if the reason exists in set of recorded reasons.
     *
     * @since 1.0.0
     * @uncommitted
     */
    [[nodiscard]] auto retried_because_of(retry_reason reason) const -> bool override;

    [[nodiscard]] auto index_name() const -> const std::string&;

    [[nodiscard]] auto client_context_id() const -> const std::string&;

    [[nodiscard]] auto query() const -> const std::string&;

    [[nodiscard]] auto parameters() const -> const std::optional<std::string>&;

    [[nodiscard]] auto method() const -> const std::string&;

    [[nodiscard]] auto path() const -> const std::string&;

    [[nodiscard]] auto http_status() const -> std::uint32_t;

    [[nodiscard]] auto http_body() const -> const std::string&;

    [[nodiscard]] auto hostname() const -> const std::string&;

    [[nodiscard]] auto port() const -> std::uint16_t;

    [[nodiscard]] auto error() const -> const std::string&;

    [[nodiscard]] auto status() const -> const std::string&;

  private:
    std::unique_ptr<internal_search_error_context> internal_;
};
} // namespace couchbase
