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

// User represents the User data structure.
// User represents a Navidrome user with minimal information exposed to plugins.
// Sensitive fields like password, email, and internal IDs are intentionally excluded.
type User struct {
	UserName string `json:"userName"`
	Name     string `json:"name"`
	IsAdmin  bool   `json:"isAdmin"`
}

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

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

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

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

// UsersGetUsers calls the users_getusers host function.
// GetUsers returns all users the plugin has been granted access to.
// Only minimal user information (userName, name, isAdmin) is returned.
// Sensitive fields like password and email are never exposed.
//
// Returns a slice of users the plugin can access, or an empty slice if none configured.
func UsersGetUsers() ([]User, error) {
	// No parameters - allocate empty JSON object
	reqMem := pdk.AllocateBytes([]byte("{}"))
	defer reqMem.Free()

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

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

	// Parse the response
	var response usersGetUsersResponse
	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
}

// UsersGetAdmins calls the users_getadmins host function.
// GetAdmins returns only admin users the plugin has been granted access to.
// This is a convenience method that filters GetUsers results to include only admins.
//
// Returns a slice of admin users the plugin can access, or an empty slice if none.
func UsersGetAdmins() ([]User, error) {
	// No parameters - allocate empty JSON object
	reqMem := pdk.AllocateBytes([]byte("{}"))
	defer reqMem.Free()

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

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

	// Parse the response
	var response usersGetAdminsResponse
	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
}
