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

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jeesharp.business.modules.member.application.MemberVipApplicationService;
import com.jeesharp.business.modules.member.dto.MemberVipSaveDTO;
import com.jeesharp.business.modules.member.entity.MemberVip;
import com.jeesharp.business.modules.member.vo.MemberVipInfoVO;
import com.jeesharp.business.modules.member.vo.MemberVipQueryVO;
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.Parameter;
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.List;
import java.util.Map;

/**
 * 会员付费开通管理 Controller
 *
 * @author jeesharp
 * @version 2026-03-19
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/member/memberVip/v1")
@Tag(name = "会员付费开通", description = "会员付费开通管理，包括开通、取消、查询等")
@AllArgsConstructor
public class MemberVipController extends BaseController {

    private final MemberVipApplicationService memberVipApplicationService;

    @ApiOperationSupport(order = 0)
    @Operation(summary = "获取付费等级选项", description = "获取付费等级下拉选项，用于开通时选择")
    @GetMapping(value = "paidLevelOptions")
    public AjaxResult<List<Map<String, String>>> paidLevelOptions() {
        List<Map<String, String>> options = memberVipApplicationService.getPaidLevelOptions();
        return AjaxResult.okData(options);
    }

    @PreAuthorize("@ss.hasPermission('member:memberVip:info')")
    @Operation(summary = "获取付费开通记录详情")
    @GetMapping(value = "info")
    public AjaxResult<MemberVipInfoVO> info(
            @Parameter(description = "记录ID", required = true)
            @RequestParam("id") String id) {
        MemberVipInfoVO vo = memberVipApplicationService.getMemberVip(id);
        return AjaxResult.okData(vo);
    }

    @PreAuthorize("@ss.hasPermission('member:memberVip:list')")
    @Operation(summary = "分页查询付费开通记录")
    @PostMapping(value = "list")
    public AjaxResult<PageRespVO<MemberVipInfoVO>> list(@RequestBody MemberVipQueryVO query) {
        Page<MemberVip> page = new Page<>(query);
        PageRespVO<MemberVipInfoVO> result = memberVipApplicationService.findPage(page, query);
        return AjaxResult.okData(result);
    }

    @PreAuthorize("@ss.hasPermission('member:memberVip:open')")
    @Operation(summary = "开通付费会员")
    @PostMapping(value = "open")
    public AjaxResult<MemberVipInfoVO> open(@RequestBody @Valid MemberVipSaveDTO dto) {
        MemberVipInfoVO vo = memberVipApplicationService.openVip(dto);
        return AjaxResult.ok("开通成功", vo);
    }

    @PreAuthorize("@ss.hasPermission('member:memberVip:cancel')")
    @Operation(summary = "取消付费开通")
    @GetMapping(value = "cancel")
    public AjaxResult<Void> cancel(
            @Parameter(description = "记录ID", required = true)
            @RequestParam("id") String id) {
        memberVipApplicationService.cancelVip(id);
        return AjaxResult.ok("取消成功");
    }

    @PreAuthorize("@ss.hasPermission('member:memberVip:list')")
    @Operation(summary = "查询某会员的开通历史")
    @GetMapping(value = "listByMemberId")
    public AjaxResult<List<MemberVipInfoVO>> listByMemberId(
            @Parameter(description = "会员ID", required = true)
            @RequestParam("memberId") String memberId) {
        List<MemberVipInfoVO> list = memberVipApplicationService.listByMemberId(memberId);
        return AjaxResult.okData(list);
    }
}
