package shop.alien.storeplatform.controller;

import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.storeplatform.service.CouponManageService;

import java.util.Map;

/**
 * web端商户优惠券管理 前端控制器
 *
 * @author ssk
 * @since 2025-11-13
 */
@Slf4j
@Api(tags = {"web端商户优惠券管理"})
@ApiSort(7)
@CrossOrigin
@RestController
@RequestMapping("/couponManage")
@RequiredArgsConstructor
public class CouponManageController {

    private final CouponManageService couponManageService;

    @ApiOperation("核销订单前效验")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderCode", value = "劵code", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/verifyOrder")
    public R<String> verifyOrder(@RequestParam("orderCode") String orderCode) {
        log.info("CouponManageController.verifyOrder?orderCode={}", orderCode);
        try {
            return couponManageService.verifyOrder(orderCode);
        } catch (Exception e) {
            log.error("CouponManageController.verifyOrder ERROR: {}", e.getMessage(), e);
            return R.fail("效验失败：" + e.getMessage());
        }
    }

    @ApiOperation("核销订单")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "quanCode", value = "券码", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/verifyCoupon")
    public R<Map<String, String>> verifyCoupon(
                                                @RequestParam("quanCode") String quanCode) {
        log.info("CouponManageController.verifyCoupon? quanCode={}", quanCode);
        try {
            Map<String, String> result = couponManageService.verifyCoupon(quanCode);
            return R.data(result);
        } catch (Exception e) {
            log.error("CouponManageController.verifyCoupon ERROR: {}", e.getMessage(), e);
            return R.fail("核销失败：" + e.getMessage());
        }
    }
}

