#pragma once

#include <cstddef>
#include <functional>
#include <optional>
#include <string>
#include <string_view>

namespace smartcut::core {

inline constexpr std::size_t control_ipc_max_message = 16U * 1024U * 1024U;
using ControlRequestHandler = std::function<std::string(std::string_view)>;

// Serves one connection per request. A zero request limit serves until a
// request containing the v1 shutdown command is received.
int serve_control_pipe(std::string_view pipe_name, const ControlRequestHandler& handler,
                       std::size_t request_limit = 0, unsigned connect_timeout_ms = 15000);

// Connects to a local named pipe, writes one little-endian uint32 length plus
// UTF-8 JSON payload, and reads one response using the same framing.
std::optional<std::string> request_control_pipe(std::string_view pipe_name,
                                                std::string_view request_json,
                                                unsigned timeout_ms = 5000);

}  // namespace smartcut::core
