package mx.gob.jumapacelaya.services;

import jakarta.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;


@Service
public class EmailService {

    @Autowired
    private JavaMailSender mailSender;

    private static final Logger log = LoggerFactory.getLogger(EmailService.class);

    public void enviarCorreo(String destinatario, String asunto, String cuerpo, String imagePath) {
       try {
           MimeMessage mensaje = mailSender.createMimeMessage();
           MimeMessageHelper helper = new MimeMessageHelper(mensaje, true, "UTF-8");
           helper.setTo(destinatario);
           helper.setSubject(asunto);
           helper.setFrom("noreply@jumapacelaya.gob.mx");
           helper.setText(cuerpo, true);

           ClassPathResource imgResource = new ClassPathResource(imagePath);

           helper.addInline("image_id", imgResource);

           mailSender.send(mensaje);
           log.info("Correo enviado a {}", destinatario);


       } catch (Exception e) {
           log.error("Error al enviar el correo: ", e);
           throw new RuntimeException("Error al enviar el correo: " + e.getMessage());
       }
    }
}
