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());
        request.setPreviewOnly(true);
        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);
    }

    /**
     * 查询用户可用优惠券
     */
    @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);
    }
}