package shop.alien.store.controller;

import io.swagger.annotations.*;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.store.util.ai.AiFeedbackEmailUtil;

/**
 * 邮件分发Controller
 * 提供反馈邮件发送接口
 *
 * @author system
 * @date 2025-01-20
 */
@Slf4j
@Api(tags = {"邮件分发接口"})
@CrossOrigin
@RestController
@RequestMapping("/feedback-email")
@RequiredArgsConstructor
public class FeedbackEmailController {

    private final AiFeedbackEmailUtil aiFeedbackEmailUtil;

    @ApiOperation(value = "发送反馈邮件", httpMethod = "POST")
    @ApiOperationSupport(order = 1)
    @PostMapping("/send")
    public R<EmailSendResponse> sendFeedbackEmail(@RequestBody FeedbackEmailRequest request) {
        log.info("FeedbackEmailController.sendFeedbackEmail, request={}", request);

        // 参数校验
        if (request == null || !StringUtils.hasText(request.getFeedbackContent())) {
            log.error("反馈内容不能为空");
            return R.fail("反馈内容不能为空");
        }

        try {
            // 调用邮件发送工具类
            AiFeedbackEmailUtil.EmailSendResult result = aiFeedbackEmailUtil.sendFeedbackEmail(
                    request.getFeedbackType(),
                    request.getFeedbackContent(),
                    request.getUserId(),
                    request.getFeedbackSource(),
                    request.getContactWay()
            );

            if (result.isSuccess()) {
                EmailSendResponse response = new EmailSendResponse();
                response.setSuccess(true);
                response.setMessage(result.getMessage());
                response.setTicketId(result.getTicketId());
                log.info("邮件发送成功：ticketId={}", result.getTicketId());
                return R.data(response, "邮件发送成功");
            } else {
                log.warn("邮件发送失败：{}", result.getMessage());
                return R.fail(result.getMessage());
            }
        } catch (Exception e) {
            log.error("发送反馈邮件异常", e);
            return R.fail("发送邮件异常：" + e.getMessage());
        }
    }

    /**
     * 反馈邮件请求DTO
     */
    @Data
    @ApiModel(value = "FeedbackEmailRequest", description = "反馈邮件请求参数")
    public static class FeedbackEmailRequest {
        @ApiModelProperty(value = "问题反馈类型（0-bug反馈，1-优化反馈，2-新增功能反馈）", example = "0")
        private String feedbackType;

        @ApiModelProperty(value = "反馈内容（必填）", required = true, example = "测试bug反馈邮件功能")
        private String feedbackContent;

        @ApiModelProperty(value = "用户ID（可选）", example = "1001")
        private Integer userId;

        @ApiModelProperty(value = "反馈来源（可选，0,1）", example = "0")
        private String feedbackSource;

        @ApiModelProperty(value = "联系方式（可选）", example = "test@example.com")
        private String contactWay;
    }

    /**
     * 邮件发送响应DTO
     */
    @Data
    @ApiModel(value = "EmailSendResponse", description = "邮件发送响应结果")
    public static class EmailSendResponse {
        @ApiModelProperty(value = "是否成功")
        private Boolean success;

        @ApiModelProperty(value = "响应消息")
        private String message;

        @ApiModelProperty(value = "工单ID")
        private Long ticketId;
    }
}

