package services

import (
	"sync"

	"github.com/fiskerinc/cloud-services/pkg/smtpclient"
	"github.com/fiskerinc/cloud-services/pkg/utils/envtool"
)

var smtpOnce sync.Once
var smtpInstance smtpclient.SMTPInterface

func GetSMTP() smtpclient.SMTPInterface {
	smtpOnce.Do(func() {
		if smtpInstance != nil {
			return
		}

		host := envtool.GetEnv("SMTP_HOST", "")
		port := envtool.GetEnvInt("SMTP_PORT", 587)
		user := envtool.GetEnv("SMTP_USER", "")
		pass := envtool.GetEnv("SMTP_PASSWORD", "")

		smtpInstance = smtpclient.NewSMTP(host, port)
		smtpInstance.Auth(user, pass)
	})

	return smtpInstance
}

func SetSMTP(smtp smtpclient.SMTPInterface) {
	smtpInstance = smtp
}
