package cn.nianxx.thhotel.platform.debug.control;

import cn.nianxx.thhotel.platform.debug.common.result.DebugEmlErrorResponse;
import cn.nianxx.thhotel.platform.debug.service.impl.DebugEmlSuperAgentException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

/**
 * Debug EML 接口异常转换。对外只返回安全错误摘要，不暴露邮件正文、HTML、OSS Secret 或 Provider Secret。
 */
@RestControllerAdvice(assignableTypes = DebugEmlSuperAgentController.class)
public class DebugEmlSuperAgentControllerAdvice {

    /**
     * 转换 Debug EML 业务异常。
     */
    @ExceptionHandler(DebugEmlSuperAgentException.class)
    public ResponseEntity<DebugEmlErrorResponse> handleDebugEmlException(DebugEmlSuperAgentException exception) {
        return ResponseEntity.status(exception.getStatus())
                .body(new DebugEmlErrorResponse(exception.getErrorCode(), exception.getMessage()));
    }

    /**
     * 转换缺少 multipart 文件或必填请求参数的异常。
     */
    @ExceptionHandler({MissingServletRequestPartException.class, MissingServletRequestParameterException.class})
    public ResponseEntity<DebugEmlErrorResponse> handleMissingRequestPart(Exception exception) {
        return ResponseEntity.badRequest()
                .body(new DebugEmlErrorResponse("REQUEST_FIELD_REQUIRED", "Debug EML 上传请求缺少必填字段。"));
    }
}
