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

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jeesharp.business.modules.coupon.application.MemberCouponApplicationService;
import com.jeesharp.business.modules.coupon.dto.MemberCouponCancelDTO;
import com.jeesharp.business.modules.coupon.dto.MemberCouponGrantDTO;
import com.jeesharp.business.modules.coupon.entity.MemberCoupon;
import com.jeesharp.business.modules.coupon.vo.MemberCouponInfoVO;
import com.jeesharp.business.modules.coupon.vo.MemberCouponListVO;
import com.jeesharp.business.modules.coupon.vo.MemberCouponQueryVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 管理端会员优惠券接口：分页查询、详情、批量发放与作废。
 *
 * @author jeesharp
 * @since 2026-05-07
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/coupon/memberCoupon/v1")
@Tag(name = "会员优惠券", description = "会员持有券管理")
@AllArgsConstructor
public class MemberCouponController extends BaseController {

    private final MemberCouponApplicationService memberCouponApplicationService;

    @ApiOperationSupport(order = 1)
    @Operation(summary = "分页列表")
    @PreAuthorize("@ss.hasPermission('coupon:memberCoupon:list')")
    @PostMapping("list")
    public AjaxResult<PageRespVO<MemberCouponListVO>> list(@RequestBody MemberCouponQueryVO query) {
        Page<MemberCoupon> page = new Page<>(query);
        return AjaxResult.okData(memberCouponApplicationService.pageAdmin(query, page));
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "详情")
    @PreAuthorize("@ss.hasPermission('coupon:memberCoupon:info') or @ss.hasPermission('coupon:memberCoupon:list')")
    @GetMapping("info")
    public AjaxResult<MemberCouponInfoVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(memberCouponApplicationService.getAdminInfo(id));
    }

    @ApiOperationSupport(order = 3)
    @Operation(summary = "发放")
    @PreAuthorize("@ss.hasPermission('coupon:memberCoupon:grant')")
    @PostMapping("grant")
    public AjaxResult<Map<String, Object>> grant(@RequestBody @Valid MemberCouponGrantDTO dto) {
        return AjaxResult.ok("发放完成", memberCouponApplicationService.grantByAdmin(dto));
    }

    @ApiOperationSupport(order = 4)
    @Operation(summary = "作废")
    @PreAuthorize("@ss.hasPermission('coupon:memberCoupon:cancel')")
    @PostMapping("cancel")
    public AjaxResult<Void> cancel(@RequestBody @Valid MemberCouponCancelDTO dto) {
        memberCouponApplicationService.cancelByAdmin(dto);
        return AjaxResult.ok("作废成功");
    }
}
