// 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 TinyGo.
//
//go:build wasip1

package host

import (
	"encoding/json"
	"errors"

	"github.com/navidrome/navidrome/plugins/pdk/go/pdk"
)

// subsonicapi_call is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user subsonicapi_call
func subsonicapi_call(uint64) uint64

// subsonicapi_callraw is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user subsonicapi_callraw
func subsonicapi_callraw(uint64) uint64

type subsonicAPICallRequest struct {
	Uri string `json:"uri"`
}

type subsonicAPICallResponse struct {
	ResponseJSON string `json:"responseJson,omitempty"`
	Error        string `json:"error,omitempty"`
}

type subsonicAPICallRawRequest struct {
	Uri string `json:"uri"`
}

type subsonicAPICallRawResponse struct {
	ContentType string `json:"contentType,omitempty"`
	Data        []byte `json:"data,omitempty"`
	Error       string `json:"error,omitempty"`
}

// SubsonicAPICall calls the subsonicapi_call host function.
// 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.
func SubsonicAPICall(uri string) (string, error) {
	// Marshal request to JSON
	req := subsonicAPICallRequest{
		Uri: uri,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return "", err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

	// Call the host function
	responsePtr := subsonicapi_call(reqMem.Offset())

	// Read the response from memory
	responseMem := pdk.FindMemory(responsePtr)
	responseBytes := responseMem.ReadBytes()

	// Parse the response
	var response subsonicAPICallResponse
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return "", err
	}

	// Convert Error field to Go error
	if response.Error != "" {
		return "", errors.New(response.Error)
	}

	return response.ResponseJSON, nil
}

// SubsonicAPICallRaw calls the subsonicapi_callraw host function.
// 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.
func SubsonicAPICallRaw(uri string) (string, []byte, error) {
	// Marshal request to JSON
	req := subsonicAPICallRawRequest{
		Uri: uri,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return "", nil, err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

	// Call the host function
	responsePtr := subsonicapi_callraw(reqMem.Offset())

	// Read the response from memory
	responseMem := pdk.FindMemory(responsePtr)
	responseBytes := responseMem.ReadBytes()

	// Parse the response
	var response subsonicAPICallRawResponse
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return "", nil, err
	}

	// Convert Error field to Go error
	if response.Error != "" {
		return "", nil, errors.New(response.Error)
	}

	return response.ContentType, response.Data, nil
}
