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

import cn.nianxx.thhotel.platform.access.control.AdminPermissionController;
import cn.nianxx.thhotel.platform.access.control.AdminRoleController;
import cn.nianxx.thhotel.platform.audit.control.AdminAuditLogController;
import cn.nianxx.thhotel.platform.common.exception.AdminOperationException;
import cn.nianxx.thhotel.platform.common.result.PlatformErrorResponse;
import cn.nianxx.thhotel.platform.hotel.control.AdminHotelController;
import cn.nianxx.thhotel.platform.identity.control.AdminUserController;
import cn.nianxx.thhotel.platform.navigation.control.AdminMenuController;
import cn.nianxx.thhotel.platform.security.service.impl.AdminAuthorizationException;
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、secret 或内部堆栈。
 */
@RestControllerAdvice(assignableTypes = {
        AdminUserController.class,
        AdminRoleController.class,
        AdminPermissionController.class,
        AdminAuditLogController.class,
        AdminMenuController.class,
        AdminHotelController.class
})
public class AdminControllerAdvice {

    /**
     * 处理管理后台登录和权限异常。
     */
    @ExceptionHandler(AdminAuthorizationException.class)
    public ResponseEntity<PlatformErrorResponse> handleAuthorization(AdminAuthorizationException exception) {
        return ResponseEntity.status(exception.getStatus())
                .body(new PlatformErrorResponse(exception.getErrorCode(), exception.getMessage()));
    }

    /**
     * 处理管理后台业务受控异常。
     */
    @ExceptionHandler(AdminOperationException.class)
    public ResponseEntity<PlatformErrorResponse> handleOperation(AdminOperationException exception) {
        return ResponseEntity.status(exception.getStatus())
                .body(new PlatformErrorResponse(exception.getErrorCode(), exception.getMessage()));
    }

    /**
     * 处理管理后台参数校验异常。
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<PlatformErrorResponse> handleValidation() {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                .body(new PlatformErrorResponse("ADMIN_INVALID_REQUEST", "系统管理请求参数不合法。"));
    }
}
