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.assignee.TaskAssigneeInactiveReqVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.assignee.TaskAssigneeLogPageReqVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.assignee.TaskAssigneeLogRespVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.assignee.TaskAssigneeRespVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.assignee.TaskAssigneeSaveReqVO;
import com.njcn.rdms.module.project.service.project.task.assignee.TaskAssigneeService;
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}/tasks/{taskId}")
@Validated
public class TaskAssigneeController {

    @Resource
    private TaskAssigneeService taskAssigneeService;

    @GetMapping("/assignees")
    @Operation(summary = "获取任务协办人列表（仅当前活跃）")
    public CommonResult<List<TaskAssigneeRespVO>> getAssigneeList(@PathVariable("projectId") Long projectId,
                                                                  @PathVariable("executionId") Long executionId,
                                                                  @PathVariable("taskId") Long taskId) {
        return success(taskAssigneeService.getAssigneeList(projectId, executionId, taskId));
    }

    @PostMapping("/assignees")
    @Operation(summary = "加入任务协办人")
    public CommonResult<Long> createAssignee(@PathVariable("projectId") Long projectId,
                                             @PathVariable("executionId") Long executionId,
                                             @PathVariable("taskId") Long taskId,
                                             @Valid @RequestBody TaskAssigneeSaveReqVO reqVO) {
        return success(taskAssigneeService.createAssignee(projectId, executionId, taskId, reqVO));
    }

    @PostMapping("/assignees/{assigneeId}/inactive")
    @Operation(summary = "退出任务协办人")
    public CommonResult<Boolean> inactiveAssignee(@PathVariable("projectId") Long projectId,
                                                  @PathVariable("executionId") Long executionId,
                                                  @PathVariable("taskId") Long taskId,
                                                  @PathVariable("assigneeId") Long assigneeId,
                                                  @Valid @RequestBody TaskAssigneeInactiveReqVO reqVO) {
        taskAssigneeService.inactiveAssignee(projectId, executionId, taskId, assigneeId, reqVO);
        return success(true);
    }

    @GetMapping("/assignee-logs")
    @Operation(summary = "获取任务协办人变更历史（分页）")
    public CommonResult<PageResult<TaskAssigneeLogRespVO>> getAssigneeLogPage(@PathVariable("projectId") Long projectId,
                                                                              @PathVariable("executionId") Long executionId,
                                                                              @PathVariable("taskId") Long taskId,
                                                                              @Valid TaskAssigneeLogPageReqVO reqVO) {
        return success(taskAssigneeService.getAssigneeLogPage(projectId, executionId, taskId, reqVO));
    }

}
