// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains client wrappers for the Library 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"
)

// Library represents the Library data structure.
// Library represents a music library with metadata.
type Library struct {
	ID            int32   `json:"id"`
	Name          string  `json:"name"`
	Path          string  `json:"path"`
	MountPoint    string  `json:"mountPoint"`
	LastScanAt    int64   `json:"lastScanAt"`
	TotalSongs    int32   `json:"totalSongs"`
	TotalAlbums   int32   `json:"totalAlbums"`
	TotalArtists  int32   `json:"totalArtists"`
	TotalSize     int64   `json:"totalSize"`
	TotalDuration float64 `json:"totalDuration"`
}

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

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

type libraryGetLibraryRequest struct {
	Id int32 `json:"id"`
}

type libraryGetLibraryResponse struct {
	Result *Library `json:"result,omitempty"`
	Error  string   `json:"error,omitempty"`
}

type libraryGetAllLibrariesResponse struct {
	Result []Library `json:"result,omitempty"`
	Error  string    `json:"error,omitempty"`
}

// LibraryGetLibrary calls the library_getlibrary host function.
// GetLibrary retrieves metadata for a specific library by ID.
//
// Parameters:
//   - id: The library's unique identifier
//
// Returns the library metadata, or an error if the library is not found.
func LibraryGetLibrary(id int32) (*Library, error) {
	// Marshal request to JSON
	req := libraryGetLibraryRequest{
		Id: id,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

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

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

	// Parse the response
	var response libraryGetLibraryResponse
	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.Result, nil
}

// LibraryGetAllLibraries calls the library_getalllibraries host function.
// GetAllLibraries retrieves metadata for all configured libraries.
//
// Returns a slice of all libraries with their metadata.
func LibraryGetAllLibraries() ([]Library, error) {
	// No parameters - allocate empty JSON object
	reqMem := pdk.AllocateBytes([]byte("{}"))
	defer reqMem.Free()

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

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

	// Parse the response
	var response libraryGetAllLibrariesResponse
	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.Result, nil
}
