#ifndef HTTPSESSION_H
#define HTTPSESSION_H

#include "Router/router.hpp"
#include <boost/beast/core/flat_buffer.hpp>
#include <boost/beast/core/tcp_stream.hpp>
#include <boost/beast/http/parser.hpp>
#include <boost/beast/http/string_body.hpp>
#include <boost/beast/http/write.hpp>
#include <cstdlib>
#include <memory>
#include <optional>

namespace Older {

/** Represents an established HTTP connection
 */
class HttpSession : public std::enable_shared_from_this<HttpSession> {
    void doRead();
    void onWrite(boost::beast::error_code ec, std::size_t, bool close);

public:
    using Request = boost::beast::http::request<boost::beast::http::string_body>;
    using RequestHandler = std::function<void(HttpSession &, const Request &, const boost::urls::matches &)>;
    HttpSession(boost::asio::ip::tcp::socket &&socket, const std::shared_ptr<boost::urls::router<RequestHandler>> &router);
    template <typename Response>
    void reply(Response &&response) {
        using ResponseType = typename std::decay_t<decltype(response)>;
        auto sp = std::make_shared<ResponseType>(std::forward<decltype(response)>(response));
        boost::beast::http::async_write(m_stream, *sp,
                                        [self = shared_from_this(), sp](boost::beast::error_code ec, std::size_t bytes) {
                                            self->onWrite(ec, bytes, sp->need_eof());
                                        });
    }
    void errorReply(const Request &request, boost::beast::http::status status, boost::beast::string_view message);
    boost::beast::tcp_stream::executor_type executor();
    boost::asio::ip::tcp::socket releaseSocket();
    void run();

protected:
    void onRead(const boost::beast::error_code &error, std::size_t);

private:
    boost::beast::tcp_stream m_stream;
    std::weak_ptr<boost::urls::router<RequestHandler>> m_router;
    boost::beast::flat_buffer m_buffer{std::numeric_limits<std::uint32_t>::max()};
    std::optional<boost::beast::http::request_parser<boost::beast::http::string_body>> m_parser;
};
} // namespace Older

#endif // HTTPSESSION_H