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

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

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

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

type schedulerScheduleOneTimeRequest struct {
	DelaySeconds int32  `json:"delaySeconds"`
	Payload      string `json:"payload"`
	ScheduleID   string `json:"scheduleId"`
}

type schedulerScheduleOneTimeResponse struct {
	NewScheduleID string `json:"newScheduleId,omitempty"`
	Error         string `json:"error,omitempty"`
}

type schedulerScheduleRecurringRequest struct {
	CronExpression string `json:"cronExpression"`
	Payload        string `json:"payload"`
	ScheduleID     string `json:"scheduleId"`
}

type schedulerScheduleRecurringResponse struct {
	NewScheduleID string `json:"newScheduleId,omitempty"`
	Error         string `json:"error,omitempty"`
}

type schedulerCancelScheduleRequest struct {
	ScheduleID string `json:"scheduleId"`
}

// SchedulerScheduleOneTime calls the scheduler_scheduleonetime host function.
// ScheduleOneTime schedules a one-time event to be triggered after the specified delay.
// Plugins that use this function must also implement the SchedulerCallback capability
//
// Parameters:
//   - delaySeconds: Number of seconds to wait before triggering the event
//   - payload: Data to be passed to the scheduled event handler
//   - scheduleID: Optional unique identifier for the scheduled job. If empty, one will be generated
//
// Returns the schedule ID that can be used to cancel the job, or an error if scheduling fails.
func SchedulerScheduleOneTime(delaySeconds int32, payload string, scheduleID string) (string, error) {
	// Marshal request to JSON
	req := schedulerScheduleOneTimeRequest{
		DelaySeconds: delaySeconds,
		Payload:      payload,
		ScheduleID:   scheduleID,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return "", err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

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

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

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

// SchedulerScheduleRecurring calls the scheduler_schedulerecurring host function.
// ScheduleRecurring schedules a recurring event using a cron expression.
// Plugins that use this function must also implement the SchedulerCallback capability
//
// Parameters:
//   - cronExpression: Standard cron format expression (e.g., "0 0 * * *" for daily at midnight)
//   - payload: Data to be passed to each scheduled event handler invocation
//   - scheduleID: Optional unique identifier for the scheduled job. If empty, one will be generated
//
// Returns the schedule ID that can be used to cancel the job, or an error if scheduling fails.
func SchedulerScheduleRecurring(cronExpression string, payload string, scheduleID string) (string, error) {
	// Marshal request to JSON
	req := schedulerScheduleRecurringRequest{
		CronExpression: cronExpression,
		Payload:        payload,
		ScheduleID:     scheduleID,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return "", err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

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

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

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

// SchedulerCancelSchedule calls the scheduler_cancelschedule host function.
// CancelSchedule cancels a scheduled job identified by its schedule ID.
//
// This works for both one-time and recurring schedules. Once cancelled, the job will not trigger
// any future events.
//
// Returns an error if the schedule ID is not found or if cancellation fails.
func SchedulerCancelSchedule(scheduleID string) error {
	// Marshal request to JSON
	req := schedulerCancelScheduleRequest{
		ScheduleID: scheduleID,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

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

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

	// Parse error-only response
	var response struct {
		Error string `json:"error,omitempty"`
	}
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return err
	}
	if response.Error != "" {
		return errors.New(response.Error)
	}
	return nil
}
