package com.ycwl.basic.controller.mobile;

import com.ycwl.basic.constant.BaseContextHandler;
import com.ycwl.basic.model.mobile.coupon.req.ClaimCouponReq;
import com.ycwl.basic.model.pc.coupon.entity.CouponEntity;
import com.ycwl.basic.model.pc.couponRecord.entity.CouponRecordEntity;
import com.ycwl.basic.service.mobile.AppCouponRecordService;
import com.ycwl.basic.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/mobile/coupon/v1")
public class AppCouponController {
    
    @Autowired
    private AppCouponRecordService appCouponRecordService;
    
    /**
     * 根据memberId、faceId和type查找优惠券记录
     */
    @GetMapping("/record")
    public ApiResponse<CouponRecordEntity> getCouponRecords(
            @RequestParam Long faceId,
            @RequestParam Integer type) {
        CouponRecordEntity record = appCouponRecordService.queryByMemberIdAndFaceIdAndType(Long.valueOf(BaseContextHandler.getUserId()), faceId, type);
        return ApiResponse.success(record);
    }
    
    /**
     * 领取优惠券
     */
    @PostMapping("/claim")
    public ApiResponse<CouponEntity> claimCoupon(@RequestBody ClaimCouponReq request) {
        request.setMemberId(Long.valueOf(BaseContextHandler.getUserId()));
        try {
            CouponEntity coupon = appCouponRecordService.claimCoupon(
                request.getMemberId(), 
                request.getFaceId(), 
                request.getType()
            );
            return ApiResponse.success(coupon);
        } catch (RuntimeException e) {
            return ApiResponse.fail(e.getMessage());
        }
    }
}
