package com.ycwl.basic.pricing.controller;

import com.ycwl.basic.utils.ApiResponse;
import com.ycwl.basic.pricing.dto.*;
import com.ycwl.basic.pricing.service.ICouponService;
import com.ycwl.basic.pricing.service.IPriceCalculationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 价格计算控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/pricing")
@RequiredArgsConstructor
public class PriceCalculationController {
    
    private final IPriceCalculationService priceCalculationService;
    private final ICouponService couponService;
    
    /**
     * 计算商品价格
     */
    @PostMapping("/calculate")
    public ApiResponse<PriceCalculationResult> calculatePrice(@RequestBody PriceCalculationRequest request) {
        log.info("价格计算请求: userId={}, products={}", request.getUserId(), request.getProducts().size());
        
        PriceCalculationResult result = priceCalculationService.calculatePrice(request);
        
        log.info("价格计算完成: originalAmount={}, finalAmount={}, usedCoupon={}", 
                result.getOriginalAmount(), result.getFinalAmount(), 
                result.getUsedCoupon() != null ? result.getUsedCoupon().getCouponId() : null);
        
        return ApiResponse.success(result);
    }
    
    /**
     * 使用优惠券
     * 只能通过代码处理，不能通过接口调用
     */
    @PostMapping("/coupons/use")
    @Deprecated
    public ApiResponse<CouponUseResult> useCoupon(@RequestBody CouponUseRequest request) {
//        log.info("优惠券使用请求: userId={}, couponId={}, orderId={}",
//                request.getUserId(), request.getCouponId(), request.getOrderId());
//
//        CouponUseResult result = couponService.useCoupon(request);
//
//        log.info("优惠券使用成功: couponId={}, discountAmount={}",
//                result.getCouponId(), result.getDiscountAmount());
//
//        ApiResponse<CouponUseResult> response = ApiResponse.success(result);
//        response.setMsg("优惠券使用成功");
//        return response;
        return null;
    }
    
    /**
     * 查询用户可用优惠券
     */
    @GetMapping("/coupons/my-coupons")
    public ApiResponse<List<CouponInfo>> getUserCoupons(@RequestParam Long userId) {
        log.info("查询用户可用优惠券: userId={}", userId);
        
        List<CouponInfo> coupons = couponService.getUserAvailableCoupons(userId);
        
        log.info("用户可用优惠券数量: {}", coupons.size());
        
        return ApiResponse.success(coupons);
    }
}