package com.jeesharp.business.member.controller;

import com.jeesharp.business.modules.coupon.api.MemberCouponMobileApi;
import com.jeesharp.business.modules.coupon.vo.CouponDemoVO;
import com.jeesharp.framework.memberauth.utils.MemberAuthUtils;
import com.jeesharp.framework.memberauth.web.BaseMobileController;
import com.jeesharp.framework.pojo.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 小程序消费券浏览接口（免登录可浏览模板；已登录时展示个人已领状态）。
 * <p>路径与 C 端 {@code apiBaseURL + /coupon/*} 对齐。</p>
 *
 * @author jeesharp
 * @since 2026-05-07
 */
@Slf4j
@RestController
@RequestMapping("${jeesharp.web.mobilePath}/coupon")
@Tag(name = "移动端-消费券模板", description = "消费券模板列表与详情")
@AllArgsConstructor
public class CouponCatalogMobileController extends BaseMobileController {

    private final MemberCouponMobileApi memberCouponMobileApi;

    @Operation(summary = "消费券模板列表")
    @GetMapping("/templates")
    public AjaxResult<List<CouponDemoVO>> templates() {
        String memberId = safeMemberId();
        return AjaxResult.okData(memberCouponMobileApi.listPublishedTemplatesForDemo(memberId));
    }

    @Operation(summary = "消费券模板详情")
    @GetMapping("/template/{id}")
    public AjaxResult<CouponDemoVO> templateDetail(@PathVariable("id") String id) {
        String memberId = safeMemberId();
        return AjaxResult.okData(memberCouponMobileApi.getTemplateDemo(id, memberId));
    }

    private static String safeMemberId() {
        try {
            return MemberAuthUtils.getLoginUserId();
        } catch (Exception e) {
            return null;
        }
    }
}
