package de.oaa.xxx.mail;

import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class MailService {

	private static final Logger LOGGER = LoggerFactory.getLogger(MailService.class);

	private final JavaMailSender mailSender;

	public MailService(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	public boolean send(Email email) {
		try {
			MimeMessage message = mailSender.createMimeMessage();
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email.getEmailAdresse()));
			message.setSubject(email.getTitel());
			message.setFrom(InternetAddress.parse("noreply@xxx-sphere.de")[0]);
			message.setContent(email.getText(), "text/html; charset=utf-8");
			message.addHeader("X-Mailin-Tag", "no-tracking");
			message.addHeader("X-Sib-Attributes", "{\"X-SIB-TRACKING\":\"0\"}");
			mailSender.send(message);
			return true;
		} catch (MessagingException e) {
			LOGGER.error(e.getLocalizedMessage(), e);
			return false;
		}
	}
}
