// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains export wrappers for the TaskWorker capability.
// It is intended for use in Navidrome plugins built with TinyGo.
//
//go:build wasip1

package taskworker

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

// TaskExecuteRequest is the request provided when a task is ready to execute.
type TaskExecuteRequest struct {
	// QueueName is the name of the queue this task belongs to.
	QueueName string `json:"queueName"`
	// TaskID is the unique identifier for this task.
	TaskID string `json:"taskId"`
	// Payload is the opaque data provided when the task was enqueued.
	Payload []byte `json:"payload"`
	// Attempt is the current attempt number (1-based: first attempt = 1).
	Attempt int32 `json:"attempt"`
}

// TaskWorker is the marker interface for taskworker plugins.
// Implement one or more of the provider interfaces below.
// TaskWorker provides task execution handling.
// This capability allows plugins to receive callbacks when their queued tasks
// are ready to execute. Plugins that use the taskqueue host service must
// implement this capability.
type TaskWorker interface{}

// TaskExecuteProvider provides the OnTaskExecute function.
type TaskExecuteProvider interface {
	OnTaskExecute(TaskExecuteRequest) (string, error)
} // Internal implementation holders
var (
	taskExecuteImpl func(TaskExecuteRequest) (string, error)
)

// Register registers a taskworker implementation.
// The implementation is checked for optional provider interfaces.
func Register(impl TaskWorker) {
	if p, ok := impl.(TaskExecuteProvider); ok {
		taskExecuteImpl = p.OnTaskExecute
	}
}

// NotImplementedCode is the standard return code for unimplemented functions.
// The host recognizes this and skips the plugin gracefully.
const NotImplementedCode int32 = -2

//go:wasmexport nd_task_execute
func _NdTaskExecute() int32 {
	if taskExecuteImpl == nil {
		// Return standard code - host will skip this plugin gracefully
		return NotImplementedCode
	}

	var input TaskExecuteRequest
	if err := pdk.InputJSON(&input); err != nil {
		pdk.SetError(err)
		return -1
	}

	output, err := taskExecuteImpl(input)
	if err != nil {
		pdk.SetError(err)
		return -1
	}

	if err := pdk.OutputJSON(output); err != nil {
		pdk.SetError(err)
		return -1
	}

	return 0
}
