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

import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.pojo.PageParam;
import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.module.project.controller.admin.project.execution.vo.execution.ProjectExecutionOwnerChangeReqVO;
import com.njcn.rdms.module.project.controller.admin.project.execution.vo.execution.ProjectExecutionPageReqVO;
import com.njcn.rdms.module.project.controller.admin.project.execution.vo.execution.ProjectExecutionStatusBoardReqVO;
import com.njcn.rdms.module.project.controller.admin.project.execution.vo.execution.ProjectExecutionStatusBoardRespVO;
import com.njcn.rdms.module.project.controller.admin.project.execution.vo.execution.ProjectExecutionRespVO;
import com.njcn.rdms.module.project.controller.admin.project.execution.vo.execution.ProjectExecutionCreateReqVO;
import com.njcn.rdms.module.project.controller.admin.project.execution.vo.execution.ProjectExecutionDeletePrecheckRespVO;
import com.njcn.rdms.module.project.controller.admin.project.execution.vo.execution.ProjectExecutionDeleteReqVO;
import com.njcn.rdms.module.project.controller.admin.project.execution.vo.execution.ProjectExecutionUpdateReqVO;
import com.njcn.rdms.module.project.controller.admin.project.execution.vo.execution.ProjectExecutionStatusActionReqVO;
import com.njcn.rdms.module.project.service.project.ProjectStatusBoardService;
import com.njcn.rdms.module.project.service.project.execution.ProjectExecutionService;
import io.swagger.v3.oas.annotations.Operation;
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/{projectId}/executions")
@Validated
public class ProjectExecutionController {

    @Resource
    private ProjectExecutionService projectExecutionService;
    @Resource
    private ProjectStatusBoardService projectStatusBoardService;


    @PostMapping
    @Operation(summary = "创建执行")
    public CommonResult<Long> createExecution(@PathVariable("projectId") Long projectId,
                                              @Valid @RequestBody ProjectExecutionCreateReqVO reqVO) {
        return success(projectExecutionService.createExecution(projectId, reqVO));
    }

    @PutMapping("/{executionId}")
    @Operation(summary = "编辑执行")
    public CommonResult<Boolean> updateExecution(@PathVariable("projectId") Long projectId,
                                                 @PathVariable("executionId") Long executionId,
                                                 @Valid @RequestBody ProjectExecutionUpdateReqVO reqVO) {
        reqVO.setId(executionId);
        projectExecutionService.updateExecution(projectId, reqVO);
        return success(true);
    }

    @GetMapping("/{executionId}")
    @Operation(summary = "获取执行详情")
    public CommonResult<ProjectExecutionRespVO> getExecution(@PathVariable("projectId") Long projectId,
                                                             @PathVariable("executionId") Long executionId) {
        return success(projectExecutionService.getExecutionRespVO(projectId, executionId));
    }

    @GetMapping("/page")
    @Operation(summary = "获取执行分页")
    public CommonResult<PageResult<ProjectExecutionRespVO>> getExecutionPage(@PathVariable("projectId") Long projectId,
                                                                             @Valid ProjectExecutionPageReqVO reqVO) {
        // 前端用负数 pageSize 表示"查询全部"，统一归一为框架约定的 PAGE_SIZE_NONE(-1)，
        // 走 BaseMapperX.selectPage 的不分页短路；@Max(200) 仅拦上界，负数不会被 validator 卡。
        if (reqVO.getPageSize() != null && reqVO.getPageSize() < 0) {
            reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        }
        return success(projectExecutionService.getExecutionRespVOPage(projectId, reqVO));
    }

    @GetMapping("/status-board")
    @Operation(summary = "获取执行状态看板")
    public CommonResult<ProjectExecutionStatusBoardRespVO> getExecutionStatusBoard(@PathVariable("projectId") Long projectId,
                                                                                   @Valid ProjectExecutionStatusBoardReqVO reqVO) {
        return success(projectStatusBoardService.getExecutionStatusBoard(projectId, reqVO));
    }

    @PostMapping("/{executionId}/change-owner")
    @Operation(summary = "变更执行负责人")
    public CommonResult<Boolean> changeOwner(@PathVariable("projectId") Long projectId,
                                             @PathVariable("executionId") Long executionId,
                                             @Valid @RequestBody ProjectExecutionOwnerChangeReqVO reqVO) {
        projectExecutionService.changeOwner(projectId, executionId, reqVO);
        return success(true);
    }

    @PostMapping("/{executionId}/change-status")
    @Operation(summary = "变更执行状态")
    public CommonResult<Boolean> changeStatus(@PathVariable("projectId") Long projectId,
                                              @PathVariable("executionId") Long executionId,
                                              @Valid @RequestBody ProjectExecutionStatusActionReqVO reqVO) {
        projectExecutionService.changeExecutionStatus(projectId, executionId, reqVO);
        return success(true);
    }

    @GetMapping("/{executionId}/delete-precheck")
    @Operation(summary = "执行删除预检：返回下挂任务数 + 是否需要重型确认")
    public CommonResult<ProjectExecutionDeletePrecheckRespVO> precheckDeleteExecution(
            @PathVariable("projectId") Long projectId,
            @PathVariable("executionId") Long executionId) {
        return success(projectExecutionService.precheckDeleteExecution(projectId, executionId));
    }

    @DeleteMapping("/{executionId}")
    @Operation(summary = "删除执行（已完成态禁止，三重确认）")
    public CommonResult<Boolean> deleteExecution(@PathVariable("projectId") Long projectId,
                                                 @PathVariable("executionId") Long executionId,
                                                 @Valid @RequestBody ProjectExecutionDeleteReqVO reqVO) {
        projectExecutionService.deleteExecution(projectId, executionId, reqVO);
        return success(true);
    }

}
