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

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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.yhy.module.core.controller.admin.quota.vo.QuotaFeeItemWithRateRespVO;
import com.yhy.module.core.service.workbench.WbUnitFeeSettingService;
import com.yhy.module.core.service.workbench.WbUnitFeeSettingService.FeeOverrideDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
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;

/**
 * 工作台 - 单位工程取费项设定 Controller
 */
@Tag(name = "工作台 - 单位工程取费项设定")
@RestController
@RequestMapping("/core/wb/unit-fee-setting")
@Validated
@RequiredArgsConstructor
public class WbUnitFeeSettingController {

    private final WbUnitFeeSettingService feeSettingService;

    @GetMapping("/list-by-unit")
    @Operation(summary = "获取单位工程的取费项列表（合并覆写值）")
    @Parameter(name = "unitId", description = "单位工程ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb:unit-fee-setting:query')")
    public CommonResult<List<QuotaFeeItemWithRateRespVO>> getListByUnitId(@RequestParam("unitId") Long unitId) {
        return success(feeSettingService.getMergedFeeList(unitId));
    }

    @GetMapping("/list-by-division")
    @Operation(summary = "根据分部分项节点ID获取取费项列表（合并覆写值）")
    @Parameter(name = "divisionId", description = "分部分项节点ID（定额节点）", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb:unit-fee-setting:query')")
    public CommonResult<List<QuotaFeeItemWithRateRespVO>> getListByDivisionId(@RequestParam("divisionId") Long divisionId) {
        return success(feeSettingService.getMergedFeeListByDivisionId(divisionId));
    }

    @PostMapping("/batch-save")
    @Operation(summary = "批量保存取费项覆写值")
    @Parameter(name = "divisionId", description = "分部分项节点ID（定额节点）", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb:unit-fee-setting:update')")
    public CommonResult<Boolean> batchSave(
            @RequestParam("divisionId") Long divisionId,
            @RequestBody List<FeeOverrideDTO> overrides) {
        feeSettingService.batchSaveOverrides(divisionId, overrides);
        return success(true);
    }

    @PostMapping("/mark-deleted")
    @Operation(summary = "标记取费项为已删除")
    @PreAuthorize("@ss.hasPermission('core:wb:unit-fee-setting:delete')")
    public CommonResult<Boolean> markAsDeleted(
            @RequestParam("divisionId") Long divisionId,
            @RequestParam("feeItemId") Long feeItemId) {
        feeSettingService.markAsDeleted(divisionId, feeItemId);
        return success(true);
    }

    @PostMapping("/initialize")
    @Operation(summary = "初始化定额节点的取费项设定（创建快照）")
    @Parameter(name = "divisionId", description = "分部分项节点ID（定额节点）", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('core:wb:unit-fee-setting:create')")
    public CommonResult<Boolean> initialize(@RequestParam("divisionId") Long divisionId) {
        feeSettingService.initializeSettings(divisionId);
        return success(true);
    }
}
