// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains client wrappers for the Config host service.
// It is intended for use in Navidrome plugins built with extism-pdk.

use extism_pdk::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct ConfigGetRequest {
    key: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ConfigGetResponse {
    #[serde(default)]
    value: String,
    #[serde(default)]
    exists: bool,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct ConfigGetIntRequest {
    key: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ConfigGetIntResponse {
    #[serde(default)]
    value: i64,
    #[serde(default)]
    exists: bool,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct ConfigKeysRequest {
    prefix: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ConfigKeysResponse {
    #[serde(default)]
    keys: Vec<String>,
}

#[host_fn]
extern "ExtismHost" {
    fn config_get(input: Json<ConfigGetRequest>) -> Json<ConfigGetResponse>;
    fn config_getint(input: Json<ConfigGetIntRequest>) -> Json<ConfigGetIntResponse>;
    fn config_keys(input: Json<ConfigKeysRequest>) -> Json<ConfigKeysResponse>;
}

/// Get retrieves a configuration value as a string.
/// 
/// Parameters:
///   - key: The configuration key
/// 
/// Returns the value and whether the key exists.
///
/// # Arguments
/// * `key` - String parameter.
///
/// # Returns
/// `Some(value)` if found, `None` otherwise.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn get(key: &str) -> Result<Option<String>, Error> {
    let response = unsafe {
        config_get(Json(ConfigGetRequest {
            key: key.to_owned(),
        }))?
    };

    if response.0.exists {
        Ok(Some(response.0.value))
    } else {
        Ok(None)
    }
}

/// GetInt retrieves a configuration value as an integer.
/// 
/// Parameters:
///   - key: The configuration key
/// 
/// Returns the value and whether the key exists. If the key exists but the
/// value cannot be parsed as an integer, exists will be false.
///
/// # Arguments
/// * `key` - String parameter.
///
/// # Returns
/// `Some(value)` if found, `None` otherwise.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn get_int(key: &str) -> Result<Option<i64>, Error> {
    let response = unsafe {
        config_getint(Json(ConfigGetIntRequest {
            key: key.to_owned(),
        }))?
    };

    if response.0.exists {
        Ok(Some(response.0.value))
    } else {
        Ok(None)
    }
}

/// Keys returns configuration keys matching the given prefix.
/// 
/// Parameters:
///   - prefix: Key prefix to filter by. If empty, returns all keys.
/// 
/// Returns a sorted slice of matching configuration keys.
///
/// # Arguments
/// * `prefix` - String parameter.
///
/// # Returns
/// The keys value.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn keys(prefix: &str) -> Result<Vec<String>, Error> {
    let response = unsafe {
        config_keys(Json(ConfigKeysRequest {
            prefix: prefix.to_owned(),
        }))?
    };

    Ok(response.0.keys)
}
