package eventjobs

import (
	"fmt"

	"git.smarteching.com/goffee/core"
	"git.smarteching.com/jacs/goffeetabler/models"
)

var SendWelcomeEmail core.EventJob = func(event *core.Event, c *core.Context) {
	go func() {
		mailer := c.GetMailer()
		logger := c.GetLogger()

		user, ok := event.Payload["user"].(models.User)
		if !ok {
			logger.Error("[SenEmail job] invalid user")
			return
		}
		mailer.SetFrom(core.EmailAddress{Name: "Goffee", Address: "mail@example.com"})
		mailer.SetTo([]core.EmailAddress{
			{
				Name: user.Name, Address: user.Email,
			},
		})
		mailer.SetSubject("Welcome To Goffee")
		body := fmt.Sprintf("Hi %v, \nWelcome to Goffe \nYour account have been created successfully. \nThanks.", user.Name)
		mailer.SetPlainTextBody(body)
		mailer.Send()
	}()
}
