// 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};

#[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,
}

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

#[link(wasm_import_module = "extism:host/user")]
extern "C" {
    fn subsonicapi_callraw(offset: u64) -> u64;
}

/// 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.
/// Optimized for binary endpoints like getCoverArt and stream that return
/// non-JSON data. The response is returned as raw bytes without JSON encoding overhead.
///
/// # Arguments
/// * `uri` - String parameter.
///
/// # Returns
/// A tuple of (content_type, data) with the raw binary response.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn call_raw(uri: &str) -> Result<(String, Vec<u8>), Error> {
    let req = SubsonicAPICallRawRequest {
        uri: uri.to_owned(),
    };
    let input_bytes = serde_json::to_vec(&req).map_err(|e| Error::msg(e.to_string()))?;
    let input_mem = Memory::from_bytes(&input_bytes).map_err(|e| Error::msg(e.to_string()))?;

    let response_offset = unsafe { subsonicapi_callraw(input_mem.offset()) };

    let response_mem = Memory::find(response_offset)
        .ok_or_else(|| Error::msg("empty response from host"))?;
    let response_bytes = response_mem.to_vec();

    if response_bytes.is_empty() {
        return Err(Error::msg("empty response from host"));
    }
    if response_bytes[0] == 0x01 {
        let msg = String::from_utf8_lossy(&response_bytes[1..]).to_string();
        return Err(Error::msg(msg));
    }
    if response_bytes[0] != 0x00 {
        return Err(Error::msg("unknown response status"));
    }
    if response_bytes.len() < 5 {
        return Err(Error::msg("malformed raw response: incomplete header"));
    }
    let ct_len = u32::from_be_bytes([
        response_bytes[1],
        response_bytes[2],
        response_bytes[3],
        response_bytes[4],
    ]) as usize;
    if ct_len > response_bytes.len() - 5 {
        return Err(Error::msg("malformed raw response: content-type overflow"));
    }
    let ct_end = 5 + ct_len;
    let content_type = String::from_utf8_lossy(&response_bytes[5..ct_end]).to_string();
    let data = response_bytes[ct_end..].to_vec();
    Ok((content_type, data))
}
