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

use extism_pdk::*;
use serde::{Deserialize, Serialize};
use base64::Engine as _;
use base64::engine::general_purpose::STANDARD as BASE64;

mod base64_bytes {
    use serde::{self, Deserialize, Deserializer, Serializer};
    use base64::Engine as _;
    use base64::engine::general_purpose::STANDARD as BASE64;

    pub fn serialize<S>(bytes: &Vec<u8>, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&BASE64.encode(bytes))
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        BASE64.decode(&s).map_err(serde::de::Error::custom)
    }
}

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

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct SubsonicAPICallResponse {
    #[serde(default)]
    response_json: String,
    #[serde(default)]
    error: Option<String>,
}

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

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct SubsonicAPICallRawResponse {
    #[serde(default)]
    content_type: String,
    #[serde(default)]
    #[serde(with = "base64_bytes")]
    data: Vec<u8>,
    #[serde(default)]
    error: Option<String>,
}

#[host_fn]
extern "ExtismHost" {
    fn subsonicapi_call(input: Json<SubsonicAPICallRequest>) -> Json<SubsonicAPICallResponse>;
    fn subsonicapi_callraw(input: Json<SubsonicAPICallRawRequest>) -> Json<SubsonicAPICallRawResponse>;
}

/// Call executes a Subsonic API request and returns the JSON response.
/// 
/// The uri parameter should be the Subsonic API path without the server prefix,
/// e.g., "getAlbumList2?type=random&size=10". The response is returned as raw JSON.
///
/// # Arguments
/// * `uri` - String parameter.
///
/// # Returns
/// The response_json value.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn call(uri: &str) -> Result<String, Error> {
    let response = unsafe {
        subsonicapi_call(Json(SubsonicAPICallRequest {
            uri: uri.to_owned(),
        }))?
    };

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

    Ok(response.0.response_json)
}

/// CallRaw executes a Subsonic API request and returns the raw binary response.
/// Designed for binary endpoints like getCoverArt and stream that return
/// non-JSON data. The data is base64-encoded over JSON on the wire.
///
/// # Arguments
/// * `uri` - String parameter.
///
/// # Returns
/// A tuple of (content_type, data).
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn call_raw(uri: &str) -> Result<(String, Vec<u8>), Error> {
    let response = unsafe {
        subsonicapi_callraw(Json(SubsonicAPICallRawRequest {
            uri: uri.to_owned(),
        }))?
    };

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

    Ok((response.0.content_type, response.0.data))
}
