package notificationimp

import (
	"context"
	"strings"

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

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

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