package com.ycwl.basic.pricing.exception;

import com.ycwl.basic.utils.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 价格查询系统全局异常处理器
 */
@Slf4j
@RestControllerAdvice(basePackages = "com.ycwl.basic.pricing")
public class PricingExceptionHandler {
    
    /**
     * 处理价格计算异常
     */
    @ExceptionHandler(PriceCalculationException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ApiResponse<Void> handlePriceCalculationException(PriceCalculationException e) {
        log.error("价格计算异常", e);
        return ApiResponse.buildResponse(400, e.getMessage());
    }
    
    /**
     * 处理优惠券无效异常
     */
    @ExceptionHandler(CouponInvalidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ApiResponse<Void> handleCouponInvalidException(CouponInvalidException e) {
        log.error("优惠券无效异常", e);
        return ApiResponse.buildResponse(400, e.getMessage());
    }
    
    /**
     * 处理商品配置未找到异常
     */
    @ExceptionHandler(ProductConfigNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ApiResponse<Void> handleProductConfigNotFoundException(ProductConfigNotFoundException e) {
        log.error("商品配置未找到异常", e);
        return ApiResponse.buildResponse(404, e.getMessage());
    }
    
    /**
     * 处理非法参数异常
     */
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ApiResponse<Void> handleIllegalArgumentException(IllegalArgumentException e) {
        log.error("非法参数异常", e);
        return ApiResponse.buildResponse(400, "参数错误: " + e.getMessage());
    }
    
    /**
     * 处理券码无效异常
     */
    @ExceptionHandler(VoucherInvalidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ApiResponse<Void> handleVoucherInvalidException(VoucherInvalidException e) {
        log.error("券码无效异常", e);
        return ApiResponse.buildResponse(400, e.getMessage());
    }
    
    /**
     * 处理券码已使用异常
     */
    @ExceptionHandler(VoucherAlreadyUsedException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ApiResponse<Void> handleVoucherAlreadyUsedException(VoucherAlreadyUsedException e) {
        log.error("券码已使用异常", e);
        return ApiResponse.buildResponse(400, e.getMessage());
    }
    
    /**
     * 处理券码不可领取异常
     */
    @ExceptionHandler(VoucherNotClaimableException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ApiResponse<Void> handleVoucherNotClaimableException(VoucherNotClaimableException e) {
        log.error("券码不可领取异常", e);
        return ApiResponse.buildResponse(400, e.getMessage());
    }
    
    /**
     * 处理优惠检测异常
     */
    @ExceptionHandler(DiscountDetectionException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ApiResponse<Void> handleDiscountDetectionException(DiscountDetectionException e) {
        log.error("优惠检测异常", e);
        return ApiResponse.buildResponse(400, e.getMessage());
    }
    
    /**
     * 处理通用异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ApiResponse<Void> handleException(Exception e) {
        log.error("系统异常", e);
        return ApiResponse.buildResponse(500, "系统内部错误");
    }
}