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

#include "core/diagnostics.hxx"
#include "core/origin.hxx"
#include "core/platform/base64.h"
#include "core/utils/movable_function.hxx"
#include "http_context.hxx"
#include "http_message.hxx"
#include "http_parser.hxx"
#include "http_streaming_parser.hxx"
#include "http_streaming_response.hxx"
#include "streams.hxx"

#include "core/tls_context_provider.hxx"

#include <asio.hpp>
#include <spdlog/fmt/bundled/chrono.h>

#include <atomic>
#include <cstddef>
#include <cstdint>
#include <deque>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

namespace couchbase::core::io
{
class http_session_info
{
public:
  http_session_info(const std::string& client_id, const std::string& session_id);
  http_session_info(const std::string& client_id,
                    const std::string& session_id,
                    asio::ip::tcp::endpoint local_endpoint,
                    const asio::ip::tcp::endpoint& remote_endpoint);

  [[nodiscard]] auto remote_endpoint() const -> const asio::ip::tcp::endpoint&;
  [[nodiscard]] auto remote_address() const -> const std::string&;
  [[nodiscard]] auto local_endpoint() const -> const asio::ip::tcp::endpoint&;
  [[nodiscard]] auto local_address() const -> const std::string&;
  [[nodiscard]] auto log_prefix() const -> const std::string&;

private:
  std::string log_prefix_;
  asio::ip::tcp::endpoint remote_endpoint_{}; // connected endpoint
  std::string remote_endpoint_address_{};     // cached string with endpoint address
  asio::ip::tcp::endpoint local_endpoint_{};
  std::string local_endpoint_address_{};
};

class http_session : public std::enable_shared_from_this<http_session>
{
public:
  http_session(service_type type,
               std::string client_id,
               std::string node_uuid,
               asio::io_context& ctx,
               origin& origin,
               std::string hostname,
               std::string service,
               http_context http_ctx,
               std::uint64_t pool_generation);

  http_session(service_type type,
               std::string client_id,
               std::string node_uuid,
               asio::io_context& ctx,
               tls_context_provider& tls,
               origin& origin,
               std::string hostname,
               std::string service,
               http_context http_ctx,
               std::uint64_t pool_generation);

  ~http_session();

  auto get_executor() const -> asio::strand<asio::io_context::executor_type>;
  [[nodiscard]] auto http_context() -> couchbase::core::http_context&;
  [[nodiscard]] auto remote_address() -> std::string;
  [[nodiscard]] auto local_address() -> std::string;
  // Local and remote endpoints of the connection the current request was last handed to the socket
  // on, empty before the first write. A resent request is recorded when its write starts on the new
  // connection, not when that connection completes.
  [[nodiscard]] auto dispatched_endpoints() -> std::pair<std::string, std::string>;
  [[nodiscard]] auto remote_endpoint() -> const asio::ip::tcp::endpoint&;
  [[nodiscard]] auto diag_info() -> diag::endpoint_diag_info;
  [[nodiscard]] auto log_prefix() -> std::string;
  [[nodiscard]] auto id() const -> const std::string&;
  [[nodiscard]] auto node_uuid() const -> const std::string&;
  // The http_session_manager generation the session serves, fixed for its lifetime: the one it
  // was created in, or for a failover replacement the one of the session it replaces. close()
  // starts a new generation, and the manager neither lists nor reuses a session from an earlier
  // one. A session outside any manager passes 0.
  [[nodiscard]] auto pool_generation() const -> std::uint64_t;
  // The manager's TLS epoch when this session was created. check_in stops a session from an earlier
  // epoch: its connection may carry a client certificate that has since been replaced.
  void set_tls_epoch(std::uint64_t epoch)
  {
    tls_epoch_ = epoch;
  }
  [[nodiscard]] auto tls_epoch() const -> std::uint64_t
  {
    return tls_epoch_;
  }
  [[nodiscard]] auto credentials() const -> cluster_credentials;
  [[nodiscard]] auto is_connected() const -> bool;
  [[nodiscard]] auto type() const -> service_type;
  [[nodiscard]] auto hostname() const -> const std::string&;
  [[nodiscard]] auto port() const -> const std::string&;
  [[nodiscard]] auto endpoint() -> const asio::ip::tcp::endpoint&;

  void connect(utils::movable_function<void()>&& callback);
  void on_stop(std::function<void()> handler);
  // Marks the session stopped and tears it down on the calling thread: releases the stream and
  // posts its close to the strand, runs the connect callback, cancels the timers and the current
  // response, and runs on_stop. Only the first call does anything; a later one returns at once,
  // possibly before that teardown is done.
  void stop();

  auto keep_alive() const -> bool;
  auto is_stopped() const -> bool;

  // A stop that another object has decided on but not yet executed. It is claimed while that
  // object holds its own lock and executed after the lock is released, so between the two the
  // session is still running and still reusable-looking. http_session_manager::check_in refuses a
  // claimed session and check_out skips one. stop() does not set it, so a reader asking whether a
  // session is owed or undergoing a stop tests is_stopped() as well. The claimer must call stop():
  // a session claimed while checked out and never stopped stays in busy_sessions_ with its socket
  // open until http_session_manager::close(), and one claimed while idle stays in idle_sessions_
  // until its idle timer fires. http_session_manager::retire_sessions() claims idle sessions and
  // posts their stop; on_stop unlists them. A separate flag rather than stopped_:
  // stop() returns early when stopped_ is set, so claiming through that member would turn the
  // teardown into a no-op.
  void mark_stopping();
  [[nodiscard]] auto is_stopping() const -> bool;

  template<typename Handler>
  void write_and_subscribe(io::http_request& request, Handler&& handler)
  {
    {
      response_context ctx{ std::forward<Handler>(handler) };
      if (request.streaming) {
        ctx.parser.response.body.use_json_streaming(std::move(request.streaming.value()));
      }
      bool installed = false;
      {
        // The stopped_ test shares the section that installs the context. stop() sets stopped_
        // before its cancel_current_response() takes this mutex, so either the stop is seen here,
        // or its cancel runs after the install and completes the request. Tested outside this
        // section, a stop in between would leave an installed context that nothing completes.
        const std::scoped_lock lock(current_response_mutex_);
        if (!stopped_) {
          streaming_response_ = false;
          std::swap(current_response_, ctx);
          installed = true;
        }
      }
      if (!installed) {
        // Stopped between check-out and here, for example by the peer's FIN on the liveness read
        // or by http_session_manager::close(). A retryable cancellation, not a request left to
        // its deadline.
        return ctx.handler(errc::common::request_canceled, {});
      }
    }
    if (request.headers["connection"] == "keep-alive") {
      keep_alive_ = true;
    }
    request.headers["user-agent"] = user_agent_;
    auto creds = origin_.credentials();
    if (creds.uses_jwt()) {
      request.headers["authorization"] = fmt::format("Bearer {}", creds.jwt_token);
    } else {
      const auto credentials = fmt::format("{}:{}", creds.username, creds.password);
      request.headers["authorization"] = fmt::format(
        "Basic {}",
        base64::encode(gsl::as_bytes(gsl::span{ credentials.data(), credentials.size() })));
    }
    write(fmt::format(
      "{} {} HTTP/1.1\r\nhost: {}:{}\r\n", request.method, request.path, hostname_, service_));
    if (!request.body.empty()) {
      request.headers["content-length"] = std::to_string(request.body.size());
    }
    for (const auto& [name, value] : request.headers) {
      write(fmt::format("{}: {}\r\n", name, value));
    }
    write("\r\n");
    write(request.body);
    remember_request(creds.uses_certificate());
    flush();
  }

  void write_and_stream(
    io::http_request& request,
    utils::movable_function<void(std::error_code, io::http_streaming_response)> resp_handler,
    utils::movable_function<void()> stream_end_handler);

  void set_idle(std::chrono::milliseconds timeout);
  auto reset_idle() -> bool;

  /**
   * Reads some bytes from the body of the HTTP response. Should only be used in streaming mode.
   * The read is initiated on the session strand, so the callback may run after this returns. At
   * most one read is outstanding on the socket: a call made while one is in flight is queued and
   * started when it completes, so callbacks for one response run in call order. Calls queued past
   * the end of the response complete with no data and no more to come; calls queued behind a read
   * that fails complete with its error.
   */
  void read_some(utils::movable_function<void(std::string, bool, std::error_code)>&& callback);

  // Posts `handler` to the strand once the session holds no read_some() call. Called on the strand;
  // every handler registered before then is posted. After stop(), a read in flight completes when
  // its aborted socket read does, and the calls queued behind it complete after that; until then
  // the session holds what they captured.
  void on_reads_drained(utils::movable_function<void()>&& handler);

private:
  struct streaming_response_context {
    utils::movable_function<void(std::error_code, io::http_streaming_response)> resp_handler{};
    utils::movable_function<void()> stream_end_handler{};
    // Copied from the response, which is not stored: the response's body holds this session, so
    // storing it here would form a cycle that keeps both alive after the consumer drops the body.
    bool must_close_connection{ false };
    http_streaming_parser parser{};
    bool complete{ false };
  };

  struct response_context {
    utils::movable_function<void(std::error_code, io::http_response&&)> handler{};
    http_parser parser{};
  };

  void on_resolve(std::error_code ec, const asio::ip::tcp::resolver::results_type& endpoints);
  void do_connect(asio::ip::tcp::resolver::results_type::iterator it);
  void on_connect(const std::error_code& ec, asio::ip::tcp::resolver::results_type::iterator it);
  void initiate_connect();
  void do_read();
  using read_callback = utils::movable_function<void(std::string, bool, std::error_code)>;
  void do_read_some(read_callback&& callback);
  void finish_read_some(std::error_code ec, bool response_ended);
  void drain_ended_reads(std::shared_ptr<std::deque<read_callback>> reads);
  void notify_reads_drained();
  void do_write();
  void write(const std::vector<std::uint8_t>& buf);
  void write(const std::string_view& buf);
  void flush();
  void cancel_current_response(std::error_code ec);
  void remember_request(bool uses_certificate);
  void record_dispatch();
  auto resend_after_rejected_handshake(std::error_code ec) -> bool;
  void reconnect_to_resend();
  void invoke_connect_callback();

  service_type type_{};
  std::string client_id_;
  std::string node_uuid_;
  std::string id_;
  std::uint64_t pool_generation_;
  std::atomic<std::uint64_t> tls_epoch_{ 0 };
  asio::io_context& ctx_;
  asio::ip::tcp::resolver resolver_;
  std::unique_ptr<stream_impl> stream_;
  // The timers are built on the strand executor, and the resolve completion is bound to it: their
  // handlers close stream_ and run the connect callback, serialised with the connect and read
  // handlers that touch the same state.
  asio::steady_timer connect_deadline_timer_;
  // The strand arms and cancels connect_deadline_timer_, and stop() cancels it on its calling
  // thread. Arming tests stopped_ under this lock, as for retry_backoff_.
  std::mutex connect_deadline_timer_mutex_{};
  asio::steady_timer idle_timer_;
  // set_idle() and reset_idle() touch idle_timer_ under the manager's sessions_mutex_ from any
  // thread, and stop() cancels it on its calling thread; an asio timer is not thread-safe.
  std::mutex idle_timer_mutex_{};
  asio::steady_timer retry_backoff_;
  // The strand arms retry_backoff_ and stop() cancels it on its calling thread; an asio timer is
  // not thread-safe. Arming tests stopped_ under this lock, so a stop() either cancels the armed
  // timer or makes the arming site return.
  std::mutex retry_backoff_mutex_{};
  // Only the first certificate refusal logs at warning: the resend repeats every reconnect_backoff
  // until the deadline.
  std::atomic_bool refusal_logged_{ false };

  core::origin& origin_;
  std::string hostname_;
  std::string service_;
  std::string user_agent_;

  std::atomic_bool stopped_{ false };
  std::atomic_bool stopping_{ false };
  std::atomic_bool connected_{ false };
  std::atomic_bool keep_alive_{ false };
  std::atomic_bool reading_{ false };
  // True while the session is parked in the connection pool between requests.  A
  // read is kept armed in this state so a peer-initiated close is detected
  // promptly (see set_idle()), rather than the dead socket being reused.
  std::atomic_bool idle_{ false };

  utils::movable_function<void()> connect_callback_{};
  std::mutex connect_callback_mutex_{};
  std::function<void()> on_stop_handler_{ nullptr };

  response_context current_response_{};
  streaming_response_context current_streaming_response_{};
  bool streaming_response_{ false };
  std::mutex current_response_mutex_{};

  // Touched only on the strand. A read_some() arriving while one is in flight waits here, since
  // both would read the socket into input_buffer_.
  bool read_some_in_flight_{ false };
  // Set while a streaming response's body is pulled through read_some(), from the head until the
  // read that ends it; do_read() arms nothing meanwhile. Touched only on the strand.
  bool body_pulled_{ false };
  std::deque<read_callback> queued_reads_{};
  // Reads moved out at a clean end and still completing through drain_ended_reads(). Touched only
  // on the strand.
  std::size_t ended_drains_{ 0 };
  // Posted once read_some_in_flight_ is clear and ended_drains_ is zero. Touched only on the
  // strand.
  std::vector<utils::movable_function<void()>> reads_drained_{};

  std::array<std::uint8_t, 16384> input_buffer_{};
  std::vector<std::vector<std::uint8_t>> output_buffer_{};
  // The first request written on a new connection, kept until the first byte of its response
  // arrives. Guarded by output_buffer_mutex_.
  std::vector<std::vector<std::uint8_t>> unanswered_request_{};
  // Set by a connect, cleared by the first byte of a response.
  std::atomic_bool fresh_connection_{ false };
  std::vector<std::vector<std::uint8_t>> writing_buffer_{};
  std::mutex output_buffer_mutex_{};
  std::mutex writing_buffer_mutex_{};
  asio::ip::tcp::resolver::results_type endpoints_{};
  http_session_info info_;
  // Guards info_, dispatched_from_ and dispatched_to_.
  std::mutex info_mutex_{};
  std::string dispatched_from_{};
  std::string dispatched_to_{};
  couchbase::core::http_context http_ctx_;

  // Read by diag_info() from any thread.
  // Initialised with a value: libstdc++ 8 deletes atomic<T>::atomic() when T() is not noexcept.
  std::atomic<std::chrono::steady_clock::time_point> last_active_{
    std::chrono::steady_clock::time_point{}
  };
  // Written by stop() on its calling thread, by initiate_connect() on the connect() caller or on
  // the strand, and by on_connect() on the strand; read by diag_info() from any thread.
  std::atomic<diag::endpoint_state> state_{ diag::endpoint_state::disconnected };
};
} // namespace couchbase::core::io
