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

import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.module.project.controller.admin.project.vo.requirement.ProjectRequirementAllowedTransitionBatchRespVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.requirement.ProjectRequirementBatchReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.requirement.ProjectRequirementCloseReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.requirement.ProjectRequirementDeleteReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.requirement.ProjectRequirementModuleDeleteReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.requirement.ProjectRequirementModuleReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.requirement.ProjectRequirementModuleRespVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.requirement.ProjectRequirementPageReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.requirement.ProjectRequirementRespVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.requirement.ProjectRequirementSaveReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.requirement.ProjectRequirementSplitReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.requirement.ProjectRequirementStatusActionReqVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.requirement.ProjectRequirementStatusDictRespVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.requirement.ProjectRequirementStatusTransitionRespVO;
import com.njcn.rdms.module.project.controller.admin.project.vo.requirement.ProjectRequirementUpdateReqVO;
import com.njcn.rdms.module.project.service.project.ProjectRequirementService;
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/project/requirement")
@Validated
public class ProjectRequirementController {

    @Resource
    private ProjectRequirementService requirementService;

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

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

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

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

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

    @GetMapping("/execution-options")
    @Operation(summary = "获取执行可关联的项目需求下拉选项")
    public CommonResult<PageResult<ProjectRequirementRespVO>> getExecutionRequirementOptions(
            @Valid ProjectRequirementPageReqVO pageReqVO) {
        return success(requirementService.getExecutionRequirementOptions(pageReqVO));
    }

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

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

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

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

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

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

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

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

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

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

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

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

}
