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

import com.jeesharp.business.modules.mall.application.MallRewardLevelConfigApplicationService;
import com.jeesharp.business.modules.mall.dto.MallRewardLevelConfigSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallRewardLevelConfig;
import com.jeesharp.business.modules.mall.vo.MallRewardLevelConfigInfoVO;
import com.jeesharp.business.modules.mall.vo.MallRewardLevelConfigListVO;
import com.jeesharp.business.modules.mall.vo.MallRewardLevelConfigQueryVO;
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 lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

/**
 * 消费赠送配置管理 Controller
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/manager/business/mall/rewardLevelConfig/v1")
@Tag(name = "消费赠送配置管理")
@AllArgsConstructor
public class MallRewardLevelConfigController extends BaseController {

    private final MallRewardLevelConfigApplicationService mallRewardLevelConfigApplicationService;

    @Operation(summary = "获取详情")
    @GetMapping(value = "info")
    public AjaxResult<MallRewardLevelConfigInfoVO> info(@RequestParam("id") String id) {
        MallRewardLevelConfigInfoVO vo = mallRewardLevelConfigApplicationService.get(id);
        return AjaxResult.okData(vo);
    }

    @Operation(summary = "获取列表（分页）")
    @PostMapping(value = "list")
    public AjaxResult<PageRespVO<MallRewardLevelConfigListVO>> list(@RequestBody MallRewardLevelConfigQueryVO query) {
        PageRespVO<MallRewardLevelConfigListVO> page = mallRewardLevelConfigApplicationService.findPage(new Page<>(query), query);
        return AjaxResult.okData(page);
    }

    @Operation(summary = "保存")
    @PostMapping(value = "save")
    public AjaxResult<Void> save(@RequestBody @Valid MallRewardLevelConfigSaveDTO dto) {
        mallRewardLevelConfigApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }

    @Operation(summary = "删除")
    @GetMapping(value = "delete")
    public AjaxResult<Void> delete(@RequestParam("ids") String ids) {
        mallRewardLevelConfigApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }

    @Operation(summary = "获取所有启用配置")
    @GetMapping(value = "getEnabled")
    public AjaxResult<List<MallRewardLevelConfig>> getEnabled() {
        List<MallRewardLevelConfig> configs = mallRewardLevelConfigApplicationService.listEnabledConfigs();
        return AjaxResult.okData(configs);
    }

    @Operation(summary = "计算赠送积分")
    @PostMapping(value = "calculatePoints")
    public AjaxResult<Integer> calculatePoints(@RequestParam("memberLevelCode") String memberLevelCode,
                                     @RequestParam("orderAmount") BigDecimal orderAmount) {
        Integer points = mallRewardLevelConfigApplicationService.calculateRewardPoints(memberLevelCode, orderAmount);
        return AjaxResult.okData(points);
    }

    @Operation(summary = "计算赠送金币")
    @PostMapping(value = "calculateCoins")
    public AjaxResult<Integer> calculateCoins(@RequestParam("memberLevelCode") String memberLevelCode,
                                    @RequestParam("orderAmount") BigDecimal orderAmount) {
        Integer coins = mallRewardLevelConfigApplicationService.calculateRewardCoins(memberLevelCode, orderAmount);
        return AjaxResult.okData(coins);
    }

    @Operation(summary = "初始化默认配置")
    @PostMapping(value = "initDefault")
    public AjaxResult<Void> initDefault() {
        mallRewardLevelConfigApplicationService.initDefaultConfigs();
        return AjaxResult.ok("初始化成功");
    }
}
