// Code generated by Azure.Iot.Operations.ProtocolCompilerLib v0.10.0.0; DO NOT EDIT.
package oven

import (
	"github.com/Azure/iot-operations-sdks/go/protocol"
)

type OvenService struct {
	protocol.Listeners
	*TelemetrySender
}

type OvenClient struct {
	protocol.Listeners
	*TelemetryReceiver
}

const (
	ModelID = "dtmi:akri:samples:oven;1"
	TelemetryTopic = "akri/samples/{modelId}/{senderId}"
)

func NewOvenService(
	app *protocol.Application,
	client protocol.MqttClient,
	opts ...protocol.Option,
) (*OvenService, error) {
	var err error

	serverOpts := []protocol.Option{
		protocol.WithTopicTokenNamespace("ex:"),
		protocol.WithTopicTokens{
			"modelId":    ModelID,
			"senderId":   client.ID(),
		},
	}

	var senderOpts protocol.TelemetrySenderOptions
	senderOpts.ApplyOptions(opts, serverOpts...)

	ovenService := &OvenService{}

	ovenService.TelemetrySender, err = NewTelemetrySender(
		app,
		client,
		TelemetryTopic,
		&senderOpts,
	)
	if err != nil {
		ovenService.Close()
		return nil, err
	}

	return ovenService, nil
}

func NewOvenClient(
	app *protocol.Application,
	client protocol.MqttClient,
	telemetryHandler protocol.TelemetryHandler[TelemetryCollection],
	opts ...protocol.Option,
) (*OvenClient, error) {
	var err error

	clientOpts := []protocol.Option{
		protocol.WithTopicTokenNamespace("ex:"),
		protocol.WithTopicTokens{
			"modelId":    ModelID,
		},
	}

	var receiverOpts protocol.TelemetryReceiverOptions
	receiverOpts.ApplyOptions(opts, clientOpts...)

	ovenClient := &OvenClient{}

	ovenClient.TelemetryReceiver, err = NewTelemetryReceiver(
		app,
		client,
		TelemetryTopic,
		telemetryHandler,
		&receiverOpts,
	)
	if err != nil {
		ovenClient.Close()
		return nil, err
	}
	ovenClient.Listeners = append(ovenClient.Listeners, ovenClient.TelemetryReceiver)

	return ovenClient, nil
}
