package com.corewing.app.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 邮件发送工具类
 */
@Slf4j
@Component
public class EmailUtil {

    private final JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

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

    /**
     * 发送简单文本邮件
     *
     * @param to      收件人邮箱
     * @param subject 邮件主题
     * @param content 邮件内容
     * @return 是否发送成功
     */
    public boolean sendSimpleMail(String to, String subject, String content) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(content);

            mailSender.send(message);
            log.info("简单文本邮件发送成功, 收件人: {}, 主题: {}", to, subject);
            return true;
        } catch (Exception e) {
            log.error("简单文本邮件发送失败, 收件人: {}, 主题: {}, 异常信息: {}", to, subject, e.getMessage(), e);
            return false;
        }
    }

    /**
     * 发送HTML邮件
     *
     * @param to      收件人邮箱
     * @param subject 邮件主题
     * @param content HTML内容
     * @return 是否发送成功
     */
    public boolean sendHtmlMail(String to, String subject, String content) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");

            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);

            mailSender.send(message);
            log.info("HTML邮件发送成功, 收件人: {}, 主题: {}", to, subject);
            return true;
        } catch (MessagingException e) {
            log.error("HTML邮件发送失败, 收件人: {}, 主题: {}, 异常信息: {}", to, subject, e.getMessage(), e);
            return false;
        }
    }

    /**
     * 发送验证码邮件
     *
     * @param to   收件人邮箱
     * @param code 验证码
     * @return 是否发送成功
     */
    public boolean sendVerifyCode(String to, String code) {
        String subject = I18nUtil.getMessage("email.subject.verify.code");
        String content = buildVerifyCodeHtml(code);
        return sendHtmlMail(to, subject, content);
    }

    /**
     * 构建验证码邮件HTML内容
     *
     * @param code 验证码
     * @return HTML内容
     */
    private String buildVerifyCodeHtml(String code) {
        return "<!DOCTYPE html>" +
                "<html>" +
                "<head>" +
                "<meta charset=\"UTF-8\">" +
                "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">" +
                "<style>" +
                "* { margin: 0; padding: 0; box-sizing: border-box; }" +
                "body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; background: #f5f5f5; padding: 20px; }" +
                ".email-wrapper { max-width: 560px; margin: 0 auto; background: #ffffff; border-radius: 12px; overflow: hidden; box-shadow: 0 2px 8px rgba(0,0,0,0.08); }" +
                ".header { background: linear-gradient(120deg, #4158D0 0%, #C850C0 100%); padding: 32px 24px; text-align: center; }" +
                ".logo { font-size: 28px; font-weight: 700; color: #ffffff; letter-spacing: 1px; margin: 0; }" +
                ".content { padding: 40px 32px; }" +
                ".greeting { font-size: 16px; color: #333; margin-bottom: 24px; line-height: 1.6; }" +
                ".code-container { background: linear-gradient(135deg, #f5f7fa 0%, #f8f9fc 100%); border-radius: 8px; padding: 28px; text-align: center; margin: 24px 0; border: 1px solid #e8ecf1; }" +
                ".code-label { font-size: 13px; color: #666; margin-bottom: 12px; letter-spacing: 0.5px; }" +
                ".code { font-size: 36px; font-weight: 700; color: #4158D0; letter-spacing: 8px; font-family: 'Courier New', monospace; }" +
                ".divider { height: 1px; background: linear-gradient(to right, transparent, #e0e0e0, transparent); margin: 32px 0; }" +
                ".notice { background: #fff8f0; border-left: 3px solid #ff9800; padding: 16px; border-radius: 4px; margin: 24px 0; }" +
                ".notice-title { font-size: 14px; color: #e65100; font-weight: 600; margin-bottom: 8px; }" +
                ".notice-text { font-size: 13px; color: #666; line-height: 1.6; }" +
                ".tips { font-size: 13px; color: #888; line-height: 1.8; }" +
                ".footer { padding: 24px 32px; background: #fafafa; text-align: center; border-top: 1px solid #eee; }" +
                ".footer-text { font-size: 12px; color: #999; line-height: 1.8; }" +
                ".footer-link { color: #4158D0; text-decoration: none; }" +
                "</style>" +
                "</head>" +
                "<body>" +
                "<div class=\"email-wrapper\">" +
                "<div class=\"header\">" +
                "<h1 class=\"logo\">CoreWing</h1>" +
                "</div>" +
                "<div class=\"content\">" +
                "<div class=\"greeting\">" + I18nUtil.getMessage("email.greeting") + "</div>" +
                "<p style=\"color: #555; font-size: 14px; line-height: 1.8; margin-bottom: 20px;\">" +
                I18nUtil.getMessage("email.verify.intro") +
                "</p>" +
                "<div class=\"code-container\">" +
                "<div class=\"code-label\">" + I18nUtil.getMessage("email.verify.code.label") + "</div>" +
                "<div class=\"code\">" + code + "</div>" +
                "</div>" +
                "<div class=\"notice\">" +
                "<div class=\"notice-title\">" + I18nUtil.getMessage("email.verify.notice.title") + "</div>" +
                "<div class=\"notice-text\">" + I18nUtil.getMessage("email.verify.notice.content") + "</div>" +
                "</div>" +
                "<div class=\"divider\"></div>" +
                "<div class=\"tips\">" +
                I18nUtil.getMessage("email.verify.security.tips") + "<br>" +
                I18nUtil.getMessage("email.verify.support.tips") +
                "</div>" +
                "</div>" +
                "<div class=\"footer\">" +
                "<div class=\"footer-text\">" +
                I18nUtil.getMessage("email.footer.notice") + "<br>" +
                I18nUtil.getMessage("email.footer.copyright") +
                "</div>" +
                "</div>" +
                "</div>" +
                "</body>" +
                "</html>";
    }
}
