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

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.task.vo.ProjectTaskBoardPageReqVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.ProjectTaskBoardPageRespVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.ProjectTaskDeletePrecheckRespVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.ProjectTaskDeleteReqVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.ProjectTaskPageReqVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.ProjectTaskStatusBoardReqVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.ProjectTaskStatusBoardRespVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.ProjectTaskRespVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.ProjectTaskSaveReqVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.ProjectTaskStatusActionReqVO;
import com.njcn.rdms.module.project.service.project.ProjectStatusBoardService;
import com.njcn.rdms.module.project.service.project.task.ProjectTaskService;
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 static com.njcn.rdms.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 项目任务")
@RestController
@RequestMapping("/project/project/{projectId}/executions/{executionId}/tasks")
@Validated
public class ProjectTaskController {

    @Resource
    private ProjectTaskService projectTaskService;
    @Resource
    private ProjectStatusBoardService projectStatusBoardService;

    @PostMapping
    @Operation(summary = "创建任务")
    public CommonResult<Long> createTask(@PathVariable("projectId") Long projectId,
                                         @PathVariable("executionId") Long executionId,
                                         @Valid @RequestBody ProjectTaskSaveReqVO reqVO) {
        return success(projectTaskService.createTask(projectId, executionId, reqVO));
    }

    @PutMapping("/{taskId}")
    @Operation(summary = "编辑任务")
    public CommonResult<Boolean> updateTask(@PathVariable("projectId") Long projectId,
                                            @PathVariable("executionId") Long executionId,
                                            @PathVariable("taskId") Long taskId,
                                            @Valid @RequestBody ProjectTaskSaveReqVO reqVO) {
        reqVO.setId(taskId);
        projectTaskService.updateTask(projectId, executionId, reqVO);
        return success(true);
    }

    @GetMapping("/{taskId}")
    @Operation(summary = "获取任务详情")
    public CommonResult<ProjectTaskRespVO> getTask(@PathVariable("projectId") Long projectId,
                                                   @PathVariable("executionId") Long executionId,
                                                   @PathVariable("taskId") Long taskId) {
        return success(projectTaskService.getTaskRespVO(projectId, executionId, taskId));
    }

    @GetMapping("/page")
    @Operation(summary = "获取任务分页")
    public CommonResult<PageResult<ProjectTaskRespVO>> getTaskPage(@PathVariable("projectId") Long projectId,
                                                                   @PathVariable("executionId") Long executionId,
                                                                   @Valid ProjectTaskPageReqVO reqVO) {
        return success(projectTaskService.getTaskRespVOPage(projectId, executionId, reqVO));
    }

    @GetMapping("/status-board")
    @Operation(summary = "获取任务状态看板")
    public CommonResult<ProjectTaskStatusBoardRespVO> getTaskStatusBoard(@PathVariable("projectId") Long projectId,
                                                                         @PathVariable("executionId") Long executionId,
                                                                         @Valid ProjectTaskStatusBoardReqVO reqVO) {
        return success(projectStatusBoardService.getTaskStatusBoard(projectId, executionId, reqVO));
    }

    @GetMapping("/board-page")
    @Operation(summary = "获取任务看板分页（按状态分列 + 每列分页 + 各列总数）")
    public CommonResult<ProjectTaskBoardPageRespVO> getTaskBoardPage(@PathVariable("projectId") Long projectId,
                                                                     @PathVariable("executionId") Long executionId,
                                                                     @Valid ProjectTaskBoardPageReqVO reqVO) {
        return success(projectStatusBoardService.getTaskBoardPage(projectId, executionId, reqVO));
    }

    @PostMapping("/{taskId}/change-status")
    @Operation(summary = "变更任务状态")
    public CommonResult<Boolean> changeStatus(@PathVariable("projectId") Long projectId,
                                              @PathVariable("executionId") Long executionId,
                                              @PathVariable("taskId") Long taskId,
                                              @Valid @RequestBody ProjectTaskStatusActionReqVO reqVO) {
        projectTaskService.changeTaskStatus(projectId, executionId, taskId, reqVO);
        return success(true);
    }

    @GetMapping("/{taskId}/delete-precheck")
    @Operation(summary = "任务删除预检：返回下挂子任务数 + 工作日志数 + 是否需要重型确认")
    public CommonResult<ProjectTaskDeletePrecheckRespVO> precheckDeleteTask(
            @PathVariable("projectId") Long projectId,
            @PathVariable("executionId") Long executionId,
            @PathVariable("taskId") Long taskId) {
        return success(projectTaskService.precheckDeleteTask(projectId, executionId, taskId));
    }

    @DeleteMapping("/{taskId}")
    @Operation(summary = "删除任务（已完成态禁止，三重确认 + 多身份授权）")
    public CommonResult<Boolean> deleteTask(@PathVariable("projectId") Long projectId,
                                            @PathVariable("executionId") Long executionId,
                                            @PathVariable("taskId") Long taskId,
                                            @Valid @RequestBody ProjectTaskDeleteReqVO reqVO) {
        projectTaskService.deleteTask(projectId, executionId, taskId, reqVO);
        return success(true);
    }

}
