package de.oaa.xxx.mail;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.annotation.Resource;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

@ApplicationScoped
@Path("send")
public class EmailSenden {

	@Resource(name = "java:jboss/mail/xxx")
	private jakarta.mail.Session session;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(EmailSenden.class);

	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	public Response sendEmail(Email email) {
		try {
			final Message message = new MimeMessage(session);
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email.getEmailAdresse()));
			message.setSubject(email.getTitel());
			message.setFrom(InternetAddress.parse("noreply@xxx-bdsmgame.de")[0]);		
			message.setContent(email.getText(), "text/html; charset=utf-8");
			Transport.send(message);
			return Response.ok().build();
		} catch (MessagingException e) {
			LOGGER.error(e.getLocalizedMessage(), e);
			return Response.serverError().build();
		}
	}

}
