package com.gxwebsoft.common.system.controller;

import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.util.EmailTemplateUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 邮件测试控制器
 * 用于测试邮件模板功能
 *
 * @author WebSoft
 * @since 2024-08-28
 */
@Tag(name = "邮件测试")
@RestController
@RequestMapping("/api/email-test")
public class EmailTestController extends BaseController {

    @Resource
    private EmailTemplateUtil emailTemplateUtil;

    @Operation(summary = "测试注册成功邮件")
    @PostMapping("/register-success")
    public ApiResult<?> testRegisterSuccessEmail(@RequestParam String email) {
        try {
            emailTemplateUtil.sendRegisterSuccessEmail(
                "测试用户", 
                "13800138000", 
                "TestPassword123", 
                email, 
                getTenantId()
            );
            return success("注册成功邮件发送成功");
        } catch (Exception e) {
            return fail("邮件发送失败: " + e.getMessage());
        }
    }

    @Operation(summary = "测试密码重置邮件")
    @PostMapping("/password-reset")
    public ApiResult<?> testPasswordResetEmail(@RequestParam String email) {
        try {
            emailTemplateUtil.sendPasswordResetEmail(
                "测试用户", 
                "13800138000", 
                "NewPassword456", 
                email, 
                getTenantId()
            );
            return success("密码重置邮件发送成功");
        } catch (Exception e) {
            return fail("邮件发送失败: " + e.getMessage());
        }
    }

    @Operation(summary = "测试通知邮件")
    @PostMapping("/notification")
    public ApiResult<?> testNotificationEmail(@RequestParam String email, 
                                            @RequestParam(required = false) String title,
                                            @RequestParam(required = false) String content) {
        try {
            String emailTitle = title != null ? title : "WebSoft系统通知";
            String emailContent = content != null ? content : "这是一条测试通知消息，用于验证邮件模板功能是否正常工作。";
            
            emailTemplateUtil.sendNotificationEmailWithAction(
                emailTitle,
                emailContent,
                email,
                getTenantId(),
                "https://www.gxwebsoft.com",
                "访问官网"
            );
            return success("通知邮件发送成功");
        } catch (Exception e) {
            return fail("邮件发送失败: " + e.getMessage());
        }
    }

    @Operation(summary = "测试安全提醒邮件")
    @PostMapping("/security-alert")
    public ApiResult<?> testSecurityAlertEmail(@RequestParam String email) {
        try {
            emailTemplateUtil.sendSecurityAlertEmail(
                "测试用户", 
                "13800138000", 
                email, 
                getTenantId(),
                "异地登录检测"
            );
            return success("安全提醒邮件发送成功");
        } catch (Exception e) {
            return fail("邮件发送失败: " + e.getMessage());
        }
    }

    @Operation(summary = "测试系统维护通知邮件")
    @PostMapping("/maintenance")
    public ApiResult<?> testMaintenanceEmail(@RequestParam String email) {
        try {
            emailTemplateUtil.sendMaintenanceNotificationEmail(
                email, 
                getTenantId(),
                "2024年8月28日 23:00-24:00",
                "1小时"
            );
            return success("维护通知邮件发送成功");
        } catch (Exception e) {
            return fail("邮件发送失败: " + e.getMessage());
        }
    }
}
