package notificationimp

import (
	"context"

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

func (a *NotificationAPI) onInvitation(context context.Context, account *model.Account, invitation *model.Invitation) error {
	var link string
	var err error
	if link, err = a.dp.GetFullLink(mservice.Invitations, invitation.ID.Hex()); err != nil {
		a.logger.Warn("Failed to generate invitation link", zap.Error(err), zap.String("email", invitation.Content.Email))
		return err
	}
	mr := a.client.MailBuilder().
		AddData("InviterName", account.Name).
		AddData("Name", invitation.Content.Name).
		AddRecipient(invitation.Content.Name, invitation.Content.Email).
		SetAccountID(account.ID.Hex()).
		SetLocale(account.Locale).
		AddButton(link).
		SetTemplateID("invitation")
	if err := a.client.Send(mr); err != nil {
		a.logger.Warn("Failed to send verification email", zap.Error(err), zap.String("email", invitation.Content.Email))
		return err
	}
	a.logger.Info("Invitation email sent", zap.String("to", invitation.Content.Email), zap.String("on_behalf_of", account.Name))
	return nil
}
