package com.psg.dlsysinfo.dl_sysinfo_server.service;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

@Service
public class EmailService {
    @Value("${app.mail.from}")
    private String from;

    @Value("${app.mail.to}")
    private String to;




    private final JavaMailSender mailSender;
    private final Environment env;

    @Autowired
    public EmailService(JavaMailSender mailSender, Environment env) {
        this.mailSender = mailSender;
        this.env = env;
    }

    public void sendHtmlEmail(String subject, String htmlBody) {
        MimeMessage message = mailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(htmlBody, true); // `true` enables HTML

            mailSender.send(message);
        } catch (MessagingException e) {
            System.err.println("❌ Error sending HTML email: " + e.getMessage());
        }
    }
}
