package com.njcn.rdms.module.project.controller.admin.product;

import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.module.project.controller.admin.product.vo.requirement.*;
import com.njcn.rdms.module.project.service.product.ProductRequirementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.njcn.rdms.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 产品需求控制器
 */
@Tag(name = "管理后台 - 产品需求")
@RestController
@RequestMapping("/project/product/requirement")
@Validated
public class ProductRequirementController {

    @Resource
    private ProductRequirementService requirementService;

    @PostMapping("/create")
    @Operation(summary = "创建产品需求")
    public CommonResult<Long> createRequirement(@Valid @RequestBody ProductRequirementSaveReqVO createReqVO) {
        return success(requirementService.createRequirement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品需求")
    public CommonResult<Boolean> updateRequirement(@Valid @RequestBody ProductRequirementUpdateReqVO updateReqVO) {
        requirementService.updateRequirement(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取需求详情")
    @Parameter(name = "id", description = "需求编号", required = true, example = "1024")
    @Parameter(name = "productId", description = "产品编号", required = true, example = "1024")
    public CommonResult<ProductRequirementRespVO> getRequirement(@RequestParam("id") Long id,
                                                                 @RequestParam("productId") Long productId) {
        return success(requirementService.getRequirement(id, productId));
    }

    @GetMapping("/page")
    @Operation(summary = "获取需求分页列表")
    public CommonResult<PageResult<ProductRequirementRespVO>> getRequirementPage(@Valid ProductRequirementPageReqVO pageReqVO) {
        return success(requirementService.getRequirementPage(pageReqVO));
    }

    @GetMapping("/tree")
    @Operation(summary = "获取需求树分页列表")
    public CommonResult<PageResult<ProductRequirementRespVO>> getRequirementTree(@Valid ProductRequirementPageReqVO pageReqVO) {
        return success(requirementService.getRequirementTree(pageReqVO));
    }

    @GetMapping("/dashboard")
    @Operation(summary = "获取产品需求概览数据")
    @Parameter(name = "productId", description = "产品编号", required = true, example = "1024")
    public CommonResult<ProductRequirementDashboardRespVO> getRequirementDashboard(@RequestParam("productId") Long productId) {
        return success(requirementService.getRequirementDashboard(productId));
    }

    @PostMapping("/change-status")
    @Operation(summary = "变更需求状态")
    public CommonResult<Boolean> changeRequirementStatus(@Valid @RequestBody ProductRequirementStatusActionReqVO reqVO) {
        requirementService.changeRequirementStatus(reqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除产品需求")
    public CommonResult<Boolean> deleteRequirement(@Valid @RequestBody ProductRequirementDeleteReqVO reqVO) {
        requirementService.deleteRequirement(reqVO.getId(), reqVO.getProductId());
        return success(true);
    }

    @PostMapping("/split")
    @Operation(summary = "拆分产品需求")
    public CommonResult<Long> splitRequirement(@Valid @RequestBody ProductRequirementSplitReqVO reqVO) {
        return success(requirementService.splitRequirement(reqVO));
    }

    @PostMapping("/close")
    @Operation(summary = "关闭产品需求")
    public CommonResult<Boolean> closeRequirement(@Valid @RequestBody ProductRequirementCloseReqVO reqVO) {
        requirementService.closeRequirement(reqVO);
        return success(true);
    }

    @GetMapping("/allowed-transitions")
    @Operation(summary = "获取需求可执行的状态动作列表")
    @Parameter(name = "requirementId", description = "需求编号", required = true, example = "1024")
    @Parameter(name = "productId", description = "产品编号", required = true, example = "1024")
    public CommonResult<List<ProductRequirementStatusTransitionRespVO>> getAllowedTransitions(
            @RequestParam("requirementId") Long requirementId,
            @RequestParam("productId") Long productId) {
        return success(requirementService.getAllowedTransitions(requirementId, productId));
    }

    @PostMapping("/allowed-transitions/batch")
    @Operation(summary = "批量获取需求可执行的状态动作列表")
    public CommonResult<List<ProductRequirementAllowedTransitionBatchRespVO>> getAllowedTransitionsBatch(
            @Valid @RequestBody ProductRequirementBatchReqVO reqVO) {
        return success(requirementService.getAllowedTransitionsBatch(reqVO));
    }

    @GetMapping("/has-dispatched")
    @Operation(summary = "判断产品需求是否已分流生成项目需求")
    @Parameter(name = "requirementId", description = "需求编号", required = true, example = "1024")
    @Parameter(name = "productId", description = "产品编号", required = true, example = "1024")
    public CommonResult<Boolean> hasDispatchedProjectRequirement(
            @RequestParam("requirementId") Long requirementId,
            @RequestParam("productId") Long productId) {
        return success(requirementService.hasDispatchedProjectRequirement(requirementId, productId));
    }

    @PostMapping("/has-dispatched/batch")
    @Operation(summary = "批量判断产品需求是否已分流生成项目需求")
    public CommonResult<List<ProductRequirementHasDispatchedBatchRespVO>> hasDispatchedProjectRequirementBatch(
            @Valid @RequestBody ProductRequirementBatchReqVO reqVO) {
        return success(requirementService.hasDispatchedProjectRequirementBatch(reqVO));
    }

    @GetMapping("/dispatched-project-link")
    @Operation(summary = "获取产品需求分流后对应的项目需求跳转链接")
    @Parameter(name = "productRequirementId", description = "产品需求编号", required = true, example = "1024")
    public CommonResult<ProductRequirementDispatchedProjectLinkRespVO> getDispatchedProjectLink(
            @RequestParam("productRequirementId") Long productRequirementId) {
        return success(requirementService.getDispatchedProjectLink(productRequirementId));
    }

    @PostMapping("/module/create")
    @Operation(summary = "创建需求模块")
    public CommonResult<Long> createRequirementModule(@Valid @RequestBody ProductRequirementModuleReqVO reqVO) {
        return success(requirementService.createRequirementModule(reqVO));
    }

    @PutMapping("/module/update")
    @Operation(summary = "更新需求模块")
    public CommonResult<Boolean> updateRequirementModule(@Valid @RequestBody ProductRequirementModuleReqVO reqVO) {
        requirementService.updateRequirementModule(reqVO);
        return success(true);
    }

    @PostMapping("/module/delete")
    @Operation(summary = "删除需求模块")
    public CommonResult<Boolean> deleteRequirementModule(@Valid @RequestBody ProductRequirementModuleDeleteReqVO reqVO) {
        requirementService.deleteRequirementModule(reqVO.getId(), reqVO.getProductId());
        return success(true);
    }

    @GetMapping("/module/tree")
    @Operation(summary = "获取需求模块树")
    @Parameter(name = "productId", description = "产品编号", required = true, example = "1024")
    public CommonResult<List<ProductRequirementModuleRespVO>> getRequirementModuleTree(@RequestParam("productId") Long productId) {
        return success(requirementService.getRequirementModuleTree(productId));
    }

    @GetMapping("/status/dict")
    @Operation(summary = "获取需求所有状态字典")
    public CommonResult<List<ProductRequirementStatusDictRespVO>> getRequirementStatusDict() {
        return success(requirementService.getRequirementStatusDict());
    }

    @GetMapping("/status/dict/terminal")
    @Operation(summary = "获取需求终止态状态字典")
    public CommonResult<List<ProductRequirementStatusDictRespVO>> getRequirementTerminalStatusDict() {
        return success(requirementService.getRequirementTerminalStatusDict());
    }

}
