package mx.gob.jumapacelaya.services;

import com.vaadin.flow.component.notification.Notification;
import jakarta.activation.DataSource;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.util.ByteArrayDataSource;
import mx.gob.jumapacelaya.models.Usuario;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.InputStream;

@Service
public class EmailService {

    @Autowired
    private JavaMailSender mailSender;

    public void enviarCorreo(String destinatario, String asunto, String cuerpo, String imagePath) {
       try {
           MimeMessage mensaje = mailSender.createMimeMessage();
           MimeMessageHelper helper = new MimeMessageHelper(mensaje, true);
           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);
           System.out.println("Correo enviado con imagen exitosamente");

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