package notifications

import (
	"encoding/json"

	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"
	"go.mongodb.org/mongo-driver/v2/bson"
)

type confirmationCodePayload struct {
	AccountRef  string `json:"accountRef"`
	Destination string `json:"destination"`
	Target      string `json:"target"`
	Code        string `json:"code"`
}

type ConfirmationCodeNotification struct {
	messaging.Envelope
	payload confirmationCodePayload
}

func (ccn *ConfirmationCodeNotification) Serialize() ([]byte, error) {
	data, err := json.Marshal(ccn.payload)
	if err != nil {
		return nil, err
	}
	return ccn.Wrap(data)
}

func newConfirmationEvent(action nm.NotificationAction) model.NotificationEvent {
	return model.NewNotification(mservice.Verification, action)
}

func NewConfirmationCodeEnvelope(sender string, accountRef bson.ObjectID, destination string, target model.VerificationPurpose, code string) messaging.Envelope {
	return &ConfirmationCodeNotification{
		Envelope: messaging.CreateEnvelope(sender, newConfirmationEvent(nm.NAPending)),
		payload: confirmationCodePayload{
			AccountRef:  accountRef.Hex(),
			Destination: destination,
			Target:      string(target),
			Code:        code,
		},
	}
}
