package com.jeesharp.business.marketing.coupon.controller;

import com.jeesharp.business.modules.coupon.vo.CouponDemoVO;
import com.jeesharp.business.modules.coupon.vo.MemberCouponInfoVO;
import com.jeesharp.business.modules.coupon.vo.MemberCouponListVO;
import com.jeesharp.business.modules.marketing.api.MarketingMobileApi;
import com.jeesharp.business.modules.marketing.coupon.dto.MarketingCouponReceiveDTO;
import com.jeesharp.business.modules.marketing.coupon.dto.MarketingCouponTenantQueryDTO;
import com.jeesharp.business.modules.marketing.coupon.vo.MarketingCouponVerifyCodeVO;
import com.jeesharp.framework.memberauth.utils.MemberAuthUtils;
import com.jeesharp.framework.memberauth.web.BaseMobileController;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.query.PageRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 移动端店铺优惠券接口：可领券、领券、我的券（须已入会且 Body 带 tenantCode）。
 *
 * @author jeesharp
 * @since 2026-06-25
 */
@Slf4j
@RestController
@RequestMapping("${jeesharp.web.mobilePath}/marketing/coupon")
@Tag(name = "移动端-店铺优惠券", description = "店铺线下券领取与查询")
@AllArgsConstructor
public class MarketingCouponMobileController extends BaseMobileController {

    private final MarketingMobileApi marketingMobileApi;

    @Operation(summary = "可领券列表")
    @PostMapping("/available")
    public AjaxResult<PageRespVO<CouponDemoVO>> available(@RequestBody @Valid MarketingCouponTenantQueryDTO dto) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(marketingMobileApi.pageAvailableCoupons(memberId, dto));
    }

    @Operation(summary = "领取优惠券")
    @PostMapping("/receive")
    public AjaxResult<Map<String, Object>> receive(@RequestBody @Valid MarketingCouponReceiveDTO dto) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.ok("领取成功", marketingMobileApi.receiveCoupon(memberId, dto));
    }

    @Operation(summary = "我的优惠券（当前店铺）")
    @PostMapping("/myList")
    public AjaxResult<PageRespVO<MemberCouponListVO>> myList(@RequestBody @Valid MarketingCouponTenantQueryDTO dto) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(marketingMobileApi.pageMyCoupons(memberId, dto));
    }

    @Operation(summary = "券详情")
    @GetMapping("/detail")
    public AjaxResult<MemberCouponInfoVO> detail(
            @RequestParam("memberCouponId") String memberCouponId,
            @RequestParam("tenantCode") String tenantCode) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(marketingMobileApi.getCouponDetail(memberId, tenantCode, memberCouponId));
    }

    @Operation(summary = "券卡面详情")
    @GetMapping("/walletDetail")
    public AjaxResult<CouponDemoVO> walletDetail(
            @RequestParam("memberCouponId") String memberCouponId,
            @RequestParam("tenantCode") String tenantCode) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(marketingMobileApi.getCouponWalletDetail(memberId, tenantCode, memberCouponId));
    }

    @Operation(summary = "到店核销动态码")
    @GetMapping("/verifyCode")
    public AjaxResult<MarketingCouponVerifyCodeVO> verifyCode(
            @RequestParam("memberCouponId") String memberCouponId,
            @RequestParam("tenantCode") String tenantCode) {
        String memberId = MemberAuthUtils.getLoginUserId();
        return AjaxResult.okData(marketingMobileApi.createVerifyCode(memberId, tenantCode, memberCouponId));
    }
}
