// Code generated by ndpgen. DO NOT EDIT.

package testpkg

import (
	"context"
	"encoding/json"

	extism "github.com/extism/go-sdk"
)

// MetaGetRequest is the request type for Meta.Get.
type MetaGetRequest struct {
	Key string `json:"key"`
}

// MetaGetResponse is the response type for Meta.Get.
type MetaGetResponse struct {
	Value any    `json:"value,omitempty"`
	Error string `json:"error,omitempty"`
}

// MetaSetRequest is the request type for Meta.Set.
type MetaSetRequest struct {
	Data map[string]any `json:"data"`
}

// MetaSetResponse is the response type for Meta.Set.
type MetaSetResponse struct {
	Error string `json:"error,omitempty"`
}

// RegisterMetaHostFunctions registers Meta service host functions.
// The returned host functions should be added to the plugin's configuration.
func RegisterMetaHostFunctions(service MetaService) []extism.HostFunction {
	return []extism.HostFunction{
		newMetaGetHostFunction(service),
		newMetaSetHostFunction(service),
	}
}

func newMetaGetHostFunction(service MetaService) extism.HostFunction {
	return extism.NewHostFunctionWithStack(
		"meta_get",
		func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
			// Read JSON request from plugin memory
			reqBytes, err := p.ReadBytes(stack[0])
			if err != nil {
				metaWriteError(p, stack, err)
				return
			}
			var req MetaGetRequest
			if err := json.Unmarshal(reqBytes, &req); err != nil {
				metaWriteError(p, stack, err)
				return
			}

			// Call the service method
			value, svcErr := service.Get(ctx, req.Key)
			if svcErr != nil {
				metaWriteError(p, stack, svcErr)
				return
			}

			// Write JSON response to plugin memory
			resp := MetaGetResponse{
				Value: value,
			}
			metaWriteResponse(p, stack, resp)
		},
		[]extism.ValueType{extism.ValueTypePTR},
		[]extism.ValueType{extism.ValueTypePTR},
	)
}

func newMetaSetHostFunction(service MetaService) extism.HostFunction {
	return extism.NewHostFunctionWithStack(
		"meta_set",
		func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {
			// Read JSON request from plugin memory
			reqBytes, err := p.ReadBytes(stack[0])
			if err != nil {
				metaWriteError(p, stack, err)
				return
			}
			var req MetaSetRequest
			if err := json.Unmarshal(reqBytes, &req); err != nil {
				metaWriteError(p, stack, err)
				return
			}

			// Call the service method
			if svcErr := service.Set(ctx, req.Data); svcErr != nil {
				metaWriteError(p, stack, svcErr)
				return
			}

			// Write JSON response to plugin memory
			resp := MetaSetResponse{}
			metaWriteResponse(p, stack, resp)
		},
		[]extism.ValueType{extism.ValueTypePTR},
		[]extism.ValueType{extism.ValueTypePTR},
	)
}

// metaWriteResponse writes a JSON response to plugin memory.
func metaWriteResponse(p *extism.CurrentPlugin, stack []uint64, resp any) {
	respBytes, err := json.Marshal(resp)
	if err != nil {
		metaWriteError(p, stack, err)
		return
	}
	respPtr, err := p.WriteBytes(respBytes)
	if err != nil {
		stack[0] = 0
		return
	}
	stack[0] = respPtr
}

// metaWriteError writes an error response to plugin memory.
func metaWriteError(p *extism.CurrentPlugin, stack []uint64, err error) {
	errResp := struct {
		Error string `json:"error"`
	}{Error: err.Error()}
	respBytes, _ := json.Marshal(errResp)
	respPtr, _ := p.WriteBytes(respBytes)
	stack[0] = respPtr
}
