// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains client wrappers for the Search 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, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Result {
    pub id: String,
}

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

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct SearchFindResponse {
    #[serde(default)]
    results: Vec<Result>,
    #[serde(default)]
    total: i32,
    #[serde(default)]
    error: Option<String>,
}

#[host_fn]
extern "ExtismHost" {
    fn search_find(input: Json<SearchFindRequest>) -> Json<SearchFindResponse>;
}

/// Calls the search_find host function.
///
/// # Arguments
/// * `query` - String parameter.
///
/// # Returns
/// A tuple of (results, total).
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn find(query: &str) -> Result<(Vec<Result>, i32), Error> {
    let response = unsafe {
        search_find(Json(SearchFindRequest {
            query: query.to_owned(),
        }))?
    };

    if let Some(err) = response.0.error {
        return Err(Error::msg(err));
    }

    Ok((response.0.results, response.0.total))
}
