// NOTE this is a generated file! do not edit!

package automod_terms_moderation_action

import (
	"context"

	"github.com/golang/protobuf/proto"

	eventbus "code.justin.tv/eventbus/client"

	"code.justin.tv/eventbus/schema/pkg/eventbus/authorization"
	"code.justin.tv/eventbus/client/encryption"
)

const (
	CreateEventType = "AutomodTermsModerationActionCreate"
)

type Create = AutomodTermsModerationActionCreate

type AutomodTermsModerationActionCreateHandler func(context.Context, *eventbus.Header, *AutomodTermsModerationActionCreate) error

func (h AutomodTermsModerationActionCreateHandler) Handler() eventbus.Handler {
	return func(ctx context.Context, message eventbus.RawMessage) error {
		dst := &AutomodTermsModerationActionCreate{}
		err := proto.Unmarshal(message.Payload, dst)
		if err != nil {
			return err
		}
		return h(ctx, message.Header, dst)
	}
}

func RegisterAutomodTermsModerationActionCreateHandler(mux *eventbus.Mux, f AutomodTermsModerationActionCreateHandler) {
	mux.RegisterHandler(CreateEventType, f.Handler())
}

func RegisterCreateHandler(mux *eventbus.Mux, f AutomodTermsModerationActionCreateHandler) {
	RegisterAutomodTermsModerationActionCreateHandler(mux, f)
}

func (*AutomodTermsModerationActionCreate) EventBusName() string {
	return CreateEventType
}
func getAutomodTermsModerationActionCreateTermTermStringAuthContext(environment string) authorization.Context {
	return authorization.Context{
		authorization.EventType:   "AutomodTermsModerationActionCreate",
		authorization.Environment: environment,
		authorization.MessageName: "Term",
		authorization.FieldName:   "TermString",
	}
}

func (m *Term) SetEncryptedTermString(p encryption.Provider, plaintext string) error {
	if m == nil {
		return authorization.ErrEncryptionNilReceiver
	}

	encCtx := getAutomodTermsModerationActionCreateTermTermStringAuthContext(p.Environment())
	err := authorization.ValidateContext(encCtx)
	if err != nil {
		return err
	}

	enc := p.Encrypter()
	b, err := enc.EncryptString(encCtx, plaintext)
	if err != nil {
		return err
	}
	
	m.TermString = &authorization.String{
		OlePayload: b,
	}
	return err
}

func (m *Term) GetDecryptedTermString(dec encryption.Decrypter) (string, error) {
	if m == nil {
		return "", authorization.ErrDecryptionNilReceiver
	}
	if m.GetTermString() == nil {
		return "", nil
	}
	return dec.DecryptString(m.GetTermString().GetOlePayload())
}
