//! Host functions exposed to WASM plugins under the `"bext"` module.
//!
//! These are registered via wasmtime's `linker.func_wrap()` and provide
//! the guest with capabilities like logging, storage, HTTP fetch, and
//! metric emission.
//!
//! ABI: all complex types are passed as JSON-encoded strings via (ptr, len)
//! pairs. Return values use (ptr, len) where negative len signals an error.

// Host function definitions:
//
// 1. bext.log(level_ptr, level_len, msg_ptr, msg_len)
// 2. bext.storage_get(key_ptr, key_len) -> (ptr, len)
// 3. bext.storage_set(key_ptr, key_len, val_ptr, val_len) -> i32
// 4. bext.storage_delete(key_ptr, key_len) -> i32
// 5. bext.http_fetch(req_ptr, req_len) -> (ptr, len)
// 6. bext.get_config(key_ptr, key_len) -> (ptr, len)
// 7. bext.emit_metric(name_ptr, name_len, value: f64, tags_ptr, tags_len)

/// HTTP fetch request from a WASM plugin.
#[derive(Debug, serde::Deserialize)]
pub struct FetchRequest {
    pub url: String,
    #[serde(default = "default_method")]
    pub method: String,
    #[serde(default)]
    pub headers: Vec<(String, String)>,
    #[serde(default)]
    pub body: Option<String>,
}

fn default_method() -> String {
    "GET".into()
}

/// HTTP fetch response returned to a WASM plugin.
#[derive(Debug, serde::Serialize)]
pub struct FetchResponse {
    pub status: u16,
    pub headers: Vec<(String, String)>,
    pub body: String,
}

/// Metric emitted by a WASM plugin.
#[derive(Debug, serde::Deserialize)]
pub struct MetricEvent {
    pub name: String,
    pub value: f64,
    #[serde(default)]
    pub tags: Vec<(String, String)>,
}
