package notifications

import (
	gmessaging "github.com/tech/sendico/pkg/generated/gmessaging"
	"github.com/tech/sendico/pkg/merrors"
	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 DemoRequestNotification struct {
	messaging.Envelope
	request *model.DemoRequest
}

func (drn *DemoRequestNotification) Serialize() ([]byte, error) {
	if drn.request == nil {
		return nil, merrors.InvalidArgument("demo request payload is empty")
	}
	msg := gmessaging.DemoRequestEvent{
		Name:             drn.request.Name,
		OrganizationName: drn.request.OrganizationName,
		Phone:            drn.request.Phone,
		WorkEmail:        drn.request.WorkEmail,
		PayoutVolume:     drn.request.PayoutVolume,
		Comment:          drn.request.Comment,
	}
	data, err := proto.Marshal(&msg)
	if err != nil {
		return nil, err
	}
	return drn.Envelope.Wrap(data)
}

func NewDemoRequestEvent() model.NotificationEvent {
	return model.NewNotification(mservice.Site, nm.NACreated)
}

func NewDemoRequestEnvelope(sender string, request *model.DemoRequest) messaging.Envelope {
	return &DemoRequestNotification{
		Envelope: messaging.CreateEnvelope(sender, NewDemoRequestEvent()),
		request:  request,
	}
}
