package com.yhy.module.core.controller.admin.quota;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.quota.vo.QuotaRateItemRespVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaRateItemSaveReqVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaRateItemSwapSortReqVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaRateModeNodeSaveReqVO;
import com.yhy.module.core.controller.admin.quota.vo.QuotaRateValueRulesReqVO;
import com.yhy.module.core.service.quota.QuotaRateItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
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;

/**
 * 定额费率项 Controller
 */
@Tag(name = "管理后台 - 定额费率项")
@RestController
@RequestMapping("/core/quota/rate/item")
@Validated
@RequiredArgsConstructor
public class QuotaRateItemController {

    private final QuotaRateItemService rateItemService;

    @PostMapping("/create-mode-node")
    @Operation(summary = "创建费率模式节点")
    public CommonResult<Long> createRateModeNode(@Valid @RequestBody QuotaRateModeNodeSaveReqVO createReqVO) {
        return success(rateItemService.createRateModeNode(createReqVO));
    }

    @PostMapping("/create")
    @Operation(summary = "创建费率项")
    public CommonResult<Long> createRateItem(@Validated(QuotaRateItemSaveReqVO.CreateGroup.class) @RequestBody QuotaRateItemSaveReqVO createReqVO) {
        return success(rateItemService.createRateItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新费率项")
    public CommonResult<Boolean> updateRateItem(@Validated(QuotaRateItemSaveReqVO.UpdateGroup.class) @RequestBody QuotaRateItemSaveReqVO updateReqVO) {
        rateItemService.updateRateItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除费率项")
    @Parameter(name = "id", description = "费率项ID", required = true)
    public CommonResult<Boolean> deleteRateItem(@RequestParam("id") Long id) {
        rateItemService.deleteRateItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取费率项详情")
    @Parameter(name = "id", description = "费率项ID", required = true)
    public CommonResult<QuotaRateItemRespVO> getRateItem(@RequestParam("id") Long id) {
        return success(rateItemService.getRateItemDetail(id));
    }

    @GetMapping("/list")
    @Operation(summary = "获取费率项列表")
    @Parameter(name = "catalogItemId", description = "模式节点ID", required = true)
    @Parameter(name = "parentId", description = "父节点ID")
    public CommonResult<List<QuotaRateItemRespVO>> getRateItemList(
            @RequestParam("catalogItemId") Long catalogItemId,
            @RequestParam(value = "parentId", required = false) Long parentId) {
        return success(rateItemService.getRateItemList(catalogItemId, parentId));
    }

    @GetMapping("/tree")
    @Operation(summary = "获取费率项树")
    @Parameter(name = "catalogItemId", description = "模式节点ID", required = true)
    public CommonResult<List<QuotaRateItemRespVO>> getRateItemTree(
            @RequestParam("catalogItemId") Long catalogItemId) {
        return success(rateItemService.getRateItemTree(catalogItemId));
    }

    @PostMapping("/swap-sort")
    @Operation(summary = "同级交换排序")
    public CommonResult<Boolean> swapSort(@Valid @RequestBody QuotaRateItemSwapSortReqVO swapReqVO) {
        rateItemService.swapSort(swapReqVO.getNodeId1(), swapReqVO.getNodeId2());
        return success(true);
    }

    @PostMapping("/move-node")
    @Operation(summary = "移动节点到指定位置", description = "将节点移动到目标节点之前，自动重新计算所有受影响节点的排序值")
    public CommonResult<Boolean> moveNode(@Valid @RequestBody QuotaRateItemSwapSortReqVO moveReqVO) {
        rateItemService.moveNode(moveReqVO.getNodeId1(), moveReqVO.getNodeId2());
        return success(true);
    }

    @PostMapping("/config-value-rules")
    @Operation(summary = "配置动态取值规则", description = "阶梯规则（tiers）必填，增量规则（increments）可选")
    public CommonResult<Boolean> configValueRules(@Valid @RequestBody QuotaRateValueRulesReqVO reqVO) {
        rateItemService.configValueRules(reqVO);
        return success(true);
    }

    @GetMapping("/calculate-fields")
    @Operation(summary = "计算动态字段值（默认使用阶梯规则）", 
               description = "根据基数值匹配阶梯规则获取字段值，如果配置了增量规则则在基础上叠加")
    @Parameter(name = "rateItemId", description = "费率项ID", required = true)
    @Parameter(name = "baseValue", description = "基数值（万元）", required = true)
    public CommonResult<Map<Integer, BigDecimal>> calculateDynamicFields(
            @RequestParam("rateItemId") Long rateItemId,
            @RequestParam("baseValue") BigDecimal baseValue) {
        return success(rateItemService.calculateDynamicFields(rateItemId, baseValue));
    }
}
