package notifications

import (
	gmessaging "github.com/tech/sendico/pkg/generated/gmessaging"
	messaging "github.com/tech/sendico/pkg/messaging/envelope"
	"github.com/tech/sendico/pkg/model"
	nm "github.com/tech/sendico/pkg/model/notification"
	"github.com/tech/sendico/pkg/mservice"
	"google.golang.org/protobuf/proto"
)

type NResultNotification struct {
	messaging.Envelope
	result *model.NotificationResult
}

func (nrn *NResultNotification) Serialize() ([]byte, error) {
	msg := gmessaging.NotificationSentEvent{
		UserId:     nrn.result.UserID,
		Channel:    nrn.result.Channel,
		Locale:     nrn.result.Locale,
		TemplateId: nrn.result.TemplateID,
		Status: &gmessaging.OperationResult{
			IsSuccessful:     nrn.result.Result.IsSuccessful,
			ErrorDescription: nrn.result.Result.Error,
		},
	}
	data, err := proto.Marshal(&msg)
	if err != nil {
		return nil, err
	}
	return nrn.Wrap(data)
}

func NewNRNotification() model.NotificationEvent {
	return model.NewNotification(mservice.Notifications, nm.NASent)
}

func NewNResultNotification(sender string, result *model.NotificationResult) messaging.Envelope {
	return &NResultNotification{
		Envelope: messaging.CreateEnvelope(sender, NewNRNotification()),
		result:   result,
	}
}
