package mailimp

import (
	mmail "github.com/tech/sendico/notification/internal/server/notificationimp/mail/messagebuilder"
	"github.com/tech/sendico/pkg/merrors"
)

type MessageBuilderImp struct {
	message *MessageImp
}

func (mb *MessageBuilderImp) SetAccountID(accountID string) mmail.MailBuilder {
	mb.message.accountUID = accountID
	return mb
}

func (mb *MessageBuilderImp) SetTemplateID(templateID string) mmail.MailBuilder {
	mb.message.templateID = templateID
	return mb
}

func (mb *MessageBuilderImp) SetLocale(locale string) mmail.MailBuilder {
	mb.message.locale = locale
	return mb
}

func (mb *MessageBuilderImp) AddButton(link string) mmail.MailBuilder {
	mb.message.buttonLink = link
	return mb
}

func (mb *MessageBuilderImp) AddRecipient(recipientName, recipient string) mmail.MailBuilder {
	mb.message.recipientName = recipientName
	mb.message.recipients = append(mb.message.recipients, recipient)
	return mb
}

func (mb *MessageBuilderImp) AddData(key, value string) mmail.MailBuilder {
	mb.message.parameters[key] = value
	return mb
}

func (mb *MessageBuilderImp) Build() (mmail.Message, error) {
	if len(mb.message.recipients) == 0 {
		return nil, merrors.InvalidArgument("Recipient not set", "recipients")
	}
	return mb.message, nil
}

func NewMessageBuilder() *MessageBuilderImp {
	return &MessageBuilderImp{
		message: createMessageImp(),
	}
}
