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.PageResult;
import com.njcn.rdms.module.project.controller.admin.project.execution.vo.assignee.ExecutionAssigneeInactiveReqVO;
import com.njcn.rdms.module.project.controller.admin.project.execution.vo.assignee.ExecutionAssigneeLogPageReqVO;
import com.njcn.rdms.module.project.controller.admin.project.execution.vo.assignee.ExecutionAssigneeLogRespVO;
import com.njcn.rdms.module.project.controller.admin.project.execution.vo.assignee.ExecutionAssigneeRespVO;
import com.njcn.rdms.module.project.controller.admin.project.execution.vo.assignee.ExecutionAssigneeSaveReqVO;
import com.njcn.rdms.module.project.service.project.execution.ProjectExecutionAssigneeService;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController;

import java.util.List;

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

@Tag(name = "管理后台 - 执行协办人")
@RestController
@RequestMapping("/project/project/{projectId}/executions/{executionId}")
@Validated
public class ProjectExecutionAssigneeController {

    @Resource
    private ProjectExecutionAssigneeService projectExecutionAssigneeService;

    @GetMapping("/assignees")
    @Operation(summary = "获取执行协办人列表（仅当前活跃）")
    public CommonResult<List<ExecutionAssigneeRespVO>> getExecutionAssigneeList(@PathVariable("projectId") Long projectId,
                                                                                @PathVariable("executionId") Long executionId) {
        return success(projectExecutionAssigneeService.getExecutionAssigneeList(projectId, executionId));
    }

    @PostMapping("/assignees")
    @Operation(summary = "新增执行协办人（B 模型 - 每次新增一段）")
    public CommonResult<Long> createExecutionAssignee(@PathVariable("projectId") Long projectId,
                                                      @PathVariable("executionId") Long executionId,
                                                      @Valid @RequestBody ExecutionAssigneeSaveReqVO reqVO) {
        return success(projectExecutionAssigneeService.createExecutionAssignee(projectId, executionId, reqVO));
    }

    @PostMapping("/assignees/{assigneeId}/inactive")
    @Operation(summary = "失效执行协办人（永久保留 removedReason）")
    public CommonResult<Boolean> inactiveExecutionAssignee(@PathVariable("projectId") Long projectId,
                                                           @PathVariable("executionId") Long executionId,
                                                           @PathVariable("assigneeId") Long assigneeId,
                                                           @Valid @RequestBody ExecutionAssigneeInactiveReqVO reqVO) {
        projectExecutionAssigneeService.inactiveExecutionAssignee(projectId, executionId, assigneeId, reqVO);
        return success(true);
    }

    @GetMapping("/assignee-logs")
    @Operation(summary = "获取执行协办人变更历史（分页）")
    public CommonResult<PageResult<ExecutionAssigneeLogRespVO>> getExecutionAssigneeLogPage(
            @PathVariable("projectId") Long projectId,
            @PathVariable("executionId") Long executionId,
            @Valid ExecutionAssigneeLogPageReqVO reqVO) {
        return success(projectExecutionAssigneeService.getExecutionAssigneeLogPage(projectId, executionId, reqVO));
    }

}
