package senders

import (
	"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/clients"
)

type MailLeadSender struct {
	client *clients.SmtpClient
}

func NewMailLeadSender(client *clients.SmtpClient) *MailLeadSender {
	return &MailLeadSender{client: client}
}

func (m *MailLeadSender) SendLead(data LeadData) error {
	err := m.SendMailWithAttachment(data.To.(string), data.Subject, data.Template, data.TemplateData, nil)
	if err != nil {
		return err
	}

	return nil
}

func (m *MailLeadSender) SendMailWithAttachment(recipient, subject string, emailTemplate string, data TemplateData, attachments []clients.Attachment) error {
	sanitizedData := sanitizeHTMLData(data)
	text, err := generateTextFromTemplate(sanitizedData, emailTemplate)
	if err != nil {
		return err
	}

	msg := clients.Message{
		To:          recipient,
		Subject:     subject,
		HtmlBody:    text,
		Attachments: attachments,
	}

	return m.client.MailSender(msg)
}

func (m *MailLeadSender) Name() string {
	return "mail"
}
