package notificationimp

import (
	"context"
	"strings"

	"github.com/tech/sendico/pkg/model"
	"github.com/tech/sendico/pkg/mutil/mzap"
	"go.uber.org/zap"
)

func (a *NotificationAPI) onConfirmationCode(ctx context.Context, account *model.Account, target string, purpose model.VerificationPurpose, code string) error {
	builder := a.client.MailBuilder().
		AddRecipient(account.Name, strings.TrimSpace(target)).
		SetAccountID(account.ID.Hex()).
		SetLocale(account.Locale).
		SetTemplateID("confirmation-code").
		AddData("Name", account.Name).
		AddData("Code", code).
		AddData("Target", model.VPToString(purpose))

	if err := a.client.Send(builder); err != nil {
		a.logger.Warn("Failed to send confirmation code email", zap.Error(err), mzap.Login(account))
		return err
	}
	a.logger.Info("Confirmation code email sent", mzap.Login(account), mzap.MaskEmail("destination", target), zap.String("target", string(purpose)))
	return nil
}
