package notificationimp

import (
	"context"

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

func (a *NotificationAPI) onAccount(context context.Context, account *model.Account, token string) error {
	var link string
	var err error
	if link, err = a.dp.GetFullLink("verify", token); err != nil {
		a.logger.Warn("Failed to generate verification link", zap.Error(err), mzap.Login(account))
		return err
	}
	mr := a.client.MailBuilder().
		AddRecipient(account.Name, account.Login).
		SetAccountID(account.ID.Hex()).
		SetLocale(account.Locale).
		AddButton(link).
		SetTemplateID("welcome")
	if err := a.client.Send(mr); err != nil {
		a.logger.Warn("Failed to send verification email", zap.Error(err), mzap.Login(account))
		return err
	}
	a.logger.Info("Verification email sent", mzap.Login(account))
	return nil
}
