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

import cn.nianxx.thhotel.platform.identity.common.result.AuthErrorResponse;
import cn.nianxx.thhotel.platform.identity.service.impl.AuthException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 登录权限接口统一异常处理。错误响应不暴露密码、token 或账号内部状态。
 */
@RestControllerAdvice(assignableTypes = AuthController.class)
public class AuthControllerAdvice {

    /**
     * 处理登录认证受控异常。
     */
    @ExceptionHandler(AuthException.class)
    public ResponseEntity<AuthErrorResponse> handleAuthException(AuthException exception) {
        return ResponseEntity.status(exception.getStatus())
                .body(new AuthErrorResponse(exception.getErrorCode(), exception.getMessage()));
    }

    /**
     * 处理请求参数校验异常。
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<AuthErrorResponse> handleValidationException() {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                .body(new AuthErrorResponse("AUTH_INVALID_REQUEST", "登录参数不完整。"));
    }
}
