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"
)

type PaymentGatewayIntentNotification struct {
	messaging.Envelope
	payload model.PaymentGatewayIntent
}

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

type PaymentGatewayExecutionNotification struct {
	messaging.Envelope
	payload model.PaymentGatewayExecution
}

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

func intentEvent() model.NotificationEvent {
	return model.NewNotification(mservice.PaymentGateway, nm.NAPaymentGatewayIntent)
}

func executionEvent() model.NotificationEvent {
	return model.NewNotification(mservice.PaymentGateway, nm.NAPaymentGatewayExecution)
}

func NewPaymentGatewayIntentEnvelope(sender string, intent *model.PaymentGatewayIntent) messaging.Envelope {
	var payload model.PaymentGatewayIntent
	if intent != nil {
		payload = *intent
	}
	return &PaymentGatewayIntentNotification{
		Envelope: messaging.CreateEnvelope(sender, intentEvent()),
		payload:  payload,
	}
}

func NewPaymentGatewayExecutionEnvelope(sender string, exec *model.PaymentGatewayExecution) messaging.Envelope {
	var payload model.PaymentGatewayExecution
	if exec != nil {
		payload = *exec
	}
	return &PaymentGatewayExecutionNotification{
		Envelope: messaging.CreateEnvelope(sender, executionEvent()),
		payload:  payload,
	}
}
