package notificationimp

import (
	"context"

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

func (a *NotificationAPI) onPasswordReset(context context.Context, account *model.Account, resetToken string) error {
	var link string
	var err error
	if link, err = a.dp.GetFullLink("password", "reset", account.ID.Hex(), resetToken); err != nil {
		a.logger.Warn("Failed to generate password reset 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).
		AddData("URL", link).
		SetTemplateID("reset-password")
	if err := a.client.Send(mr); err != nil {
		a.logger.Warn("Failed to send password reset email", zap.Error(err), mzap.Login(account))
		return err
	}
	a.logger.Info("Password reset email sent", mzap.Login(account))
	return nil
}
