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

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jeesharp.business.modules.member.application.MemberPointsRuleManageApplicationService;
import com.jeesharp.business.modules.member.dto.MemberPointsRuleSaveDTO;
import com.jeesharp.business.modules.member.entity.MemberPointsRule;
import com.jeesharp.business.modules.member.vo.MemberPointsRuleInfoVO;
import com.jeesharp.business.modules.member.vo.MemberPointsRuleListVO;
import com.jeesharp.business.modules.member.vo.MemberPointsRuleMetaLevelVO;
import com.jeesharp.business.modules.member.vo.MemberPointsRuleMetaOptionVO;
import com.jeesharp.business.modules.member.vo.MemberPointsRuleQueryVO;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 积分规则配置管理端 REST 接口。
 *
 * @author jeesharp
 * @since 2026-06-23
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/member/pointsRule/v1")
@Tag(name = "积分规则配置", description = "会员积分场景规则与等级差异配置")
@AllArgsConstructor
public class MemberPointsRuleController extends BaseController {

    private final MemberPointsRuleManageApplicationService memberPointsRuleManageApplicationService;

    @ApiOperationSupport(order = 1)
    @Operation(summary = "分页列表")
    @PreAuthorize("@ss.hasPermission('member:pointsRule:list')")
    @PostMapping("list")
    public AjaxResult<PageRespVO<MemberPointsRuleListVO>> list(@RequestBody MemberPointsRuleQueryVO query) {
        Page<MemberPointsRule> page = new Page<>(query);
        return AjaxResult.okData(memberPointsRuleManageApplicationService.findPage(page, query));
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "详情")
    @PreAuthorize("@ss.hasPermission('member:pointsRule:info')")
    @GetMapping("info")
    public AjaxResult<MemberPointsRuleInfoVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(memberPointsRuleManageApplicationService.get(id));
    }

    @ApiOperationSupport(order = 3)
    @Operation(summary = "新增")
    @PreAuthorize("@ss.hasPermission('member:pointsRule:create')")
    @PostMapping("create")
    public AjaxResult<String> create(@RequestBody @Valid MemberPointsRuleSaveDTO dto) {
        String id = memberPointsRuleManageApplicationService.create(dto);
        return AjaxResult.ok("新增成功", id);
    }

    @ApiOperationSupport(order = 4)
    @Operation(summary = "更新")
    @PreAuthorize("@ss.hasPermission('member:pointsRule:update')")
    @PostMapping("update")
    public AjaxResult<Void> update(@RequestBody @Valid MemberPointsRuleSaveDTO dto) {
        memberPointsRuleManageApplicationService.update(dto);
        return AjaxResult.ok("更新成功");
    }

    @ApiOperationSupport(order = 5)
    @Operation(summary = "删除")
    @PreAuthorize("@ss.hasPermission('member:pointsRule:delete')")
    @GetMapping("delete")
    public AjaxResult<Void> delete(@Parameter(description = "主键，逗号分隔") @RequestParam("ids") String ids) {
        memberPointsRuleManageApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }

    @ApiOperationSupport(order = 6)
    @Operation(summary = "场景编码选项")
    @PreAuthorize("@ss.hasPermission('member:pointsRule:list')")
    @GetMapping("meta/scenes")
    public AjaxResult<List<MemberPointsRuleMetaOptionVO>> metaScenes() {
        return AjaxResult.okData(memberPointsRuleManageApplicationService.metaScenes());
    }

    @ApiOperationSupport(order = 7)
    @Operation(summary = "计算方式选项")
    @PreAuthorize("@ss.hasPermission('member:pointsRule:list')")
    @GetMapping("meta/calcTypes")
    public AjaxResult<List<MemberPointsRuleMetaOptionVO>> metaCalcTypes() {
        return AjaxResult.okData(memberPointsRuleManageApplicationService.metaCalcTypes());
    }

    @ApiOperationSupport(order = 8)
    @Operation(summary = "会员等级选项")
    @PreAuthorize("@ss.hasPermission('member:pointsRule:list')")
    @GetMapping("meta/levels")
    public AjaxResult<List<MemberPointsRuleMetaLevelVO>> metaLevels() {
        return AjaxResult.okData(memberPointsRuleManageApplicationService.metaLevels());
    }
}
