// http_request.h — generic HTTP client (cURL via popen).
//
// Issue 0110. Replaces the inline curl popen patterns scattered across
// apps/services_monitor, apps/dag_engine_ui, apps/data_factory.
//
// Portable across Linux / WSL / MinGW (no link-time libcurl required —
// curl(1) must be in PATH at runtime).
#pragma once

#include <cstdint>
#include <string>
#include <utility>
#include <vector>

namespace fn_http {

struct Request {
    std::string method;                                            // "GET", "POST", "PUT", "DELETE", ...
    std::string url;                                                // full URL incl. scheme
    std::vector<std::pair<std::string, std::string>> headers;       // extra request headers
    std::string body;                                               // raw body bytes (JSON, form, etc.)
    int         timeout_ms     = 5000;                              // hard timeout (curl --max-time)
    std::string bearer_token;                                       // shortcut: Authorization: Bearer <token>
    std::string basic_user;                                         // shortcut: Authorization: Basic base64(user:pass)
    std::string basic_pass;
    bool        insecure       = false;                             // skip TLS verify (curl -k) — TESTING ONLY
};

struct Response {
    int     status      = 0;                                        // HTTP status; 0 = transport error
    std::string body;                                                // response body bytes
    std::vector<std::pair<std::string, std::string>> headers;        // response headers
    std::string error;                                               // non-empty on transport / curl failure
    int64_t duration_ms = 0;                                         // wall-clock of the call
};

// Synchronously execute `req`. Never throws. On transport failure (curl
// non-zero exit, missing binary, DNS fail) `Response.status==0` and `error`
// describes the cause. On HTTP error (4xx/5xx) `status` is set normally and
// `error` is empty — callers decide whether non-2xx is a failure.
Response request(const Request& req);

} // namespace fn_http
