package com.nexo.demo.controller;

import com.nexo.common.core.domain.R;
import com.nexo.common.mail.utils.MailUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;


/**
 * 邮件发送案例
 *
 * @author Michelle.Chung
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/demo/mail")
public class MailController {

    /**
     * 发送邮件
     *
     * @param to 接收人
     * @param subject 标题
     * @param text 内容
     */
    @GetMapping("/sendSimpleMessage")
    public R<Void> sendSimpleMessage(String to, String subject, String text) {
        text = "<!DOCTYPE html>\n" + "<html>\n" + "<head>\n" + "    <meta charset=\"UTF-8\">\n" + "    <title>邮箱验证码</title>\n" + "</head>\n" + "<body style=\"margin: 0; padding: 0; font-family: Arial, sans-serif; background-color: #f4f4f4;\">\n" + "    <table role=\"presentation\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" style=\"background-color: #f4f4f4; padding: 20px 0;\">\n" + "        <tr>\n" + "            <td align=\"center\">\n" + "                <table role=\"presentation\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\" style=\"background-color: #ffffff; border-radius: 8px; overflow: hidden; box-shadow: 0 4px 8px rgba(0,0,0,0.1);\">\n" + "                    <!-- 顶部标题栏 -->\n" + "                    <tr>\n" + "                        <td style=\"background-color: #4CAF50; color: #ffffff; padding: 20px; text-align: center;\">\n" + "                            <h2 style=\"margin: 0;\">【你的平台名称】身份验证</h2>\n" + "                        </td>\n" + "                    </tr>\n" + "                    <!-- 正文内容 -->\n" + "                    <tr>\n" + "                        <td style=\"padding: 40px 30px; text-align: center; color: #333333;\">\n" + "                            <p style=\"font-size: 16px; margin-bottom: 30px;\">亲爱的用户，您好！</p>\n" + "                            <p style=\"font-size: 16px; margin-bottom: 30px;\">您正在进行身份验证操作，本次请求的验证码为：</p>\n" + "                            <!-- 验证码高亮展示区域 -->\n" + "                            <div style=\"font-size: 36px; font-weight: bold; color: #4CAF50; letter-spacing: 5px; padding: 15px; background-color: #f9f9f9; border: 1px dashed #4CAF50; border-radius: 5px; display: inline-block;\">\n" + "                                {CODE} \n" + "                            </div>\n" + "                            <p style=\"font-size: 14px; color: #888888; margin-top: 30px;\">该验证码将在 <strong>{EXPIRE}</strong> 分钟内有效，请勿泄露给他人。</p>\n" + "                            <p style=\"font-size: 14px; color: #888888;\">如果这不是您本人的操作，请忽略此邮件。</p>\n" + "                        </td>\n" + "                    </tr>\n" + "                    <!-- 底部版权 -->\n" + "                    <tr>\n" + "                        <td style=\"background-color: #f9f9f9; padding: 15px; text-align: center; font-size: 12px; color: #aaaaaa;\">\n" + "                            &copy; 2026 你的公司/平台名称. All rights reserved.\n" + "                        </td>\n" + "                    </tr>\n" + "                </table>\n" + "            </td>\n" + "        </tr>\n" + "    </table>\n" + "</body>\n" + "</html>";
        MailUtils.sendHtml(to, subject, text);
        return R.ok();
    }

    /**
     * 发送邮件（带附件）
     *
     * @param to 接收人
     * @param subject 标题
     * @param text 内容
     * @param filePath 附件路径
     */
    @GetMapping("/sendMessageWithAttachment")
    public R<Void> sendMessageWithAttachment(String to, String subject, String text, String filePath) {
        MailUtils.sendText(to, subject, text, new File(filePath));
        return R.ok();
    }

}
