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

import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.pojo.PageResult;
import com.njcn.rdms.module.project.constant.PersonalItemConstants;
import com.njcn.rdms.module.project.controller.admin.personal.vo.item.PersonalItemPageReqVO;
import com.njcn.rdms.module.project.controller.admin.personal.vo.item.PersonalItemRespVO;
import com.njcn.rdms.module.project.controller.admin.personal.vo.item.PersonalItemSaveReqVO;
import com.njcn.rdms.module.project.controller.admin.personal.vo.item.PersonalItemStatusActionReqVO;
import com.njcn.rdms.module.project.controller.admin.project.execution.vo.execution.ProjectExecutionRespVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.worklog.TaskWorklogPageReqVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.worklog.TaskWorklogRespVO;
import com.njcn.rdms.module.project.controller.admin.project.task.vo.worklog.TaskWorklogSaveReqVO;
import com.njcn.rdms.module.project.service.personal.PersonalItemService;
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.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
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/personal-items")
@Validated
public class PersonalItemController {

    @Resource
    private PersonalItemService personalItemService;
    @Resource
    private ProjectExecutionService projectExecutionService;

    @PostMapping
    @Operation(summary = "创建个人事项")
    //@PreAuthorize("@ss.hasPermission('" + PersonalItemConstants.PERMISSION_CREATE + "')")
    public CommonResult<Long> create(@Valid @RequestBody PersonalItemSaveReqVO reqVO) {
        return success(personalItemService.createItem(reqVO));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新个人事项")
    //@PreAuthorize("@ss.hasPermission('" + PersonalItemConstants.PERMISSION_UPDATE + "')")
    public CommonResult<Boolean> update(@PathVariable("id") Long id,
                                        @Valid @RequestBody PersonalItemSaveReqVO reqVO) {
        personalItemService.updateItem(id, reqVO);
        return success(true);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取个人事项详情")
    //@PreAuthorize("@ss.hasPermission('" + PersonalItemConstants.PERMISSION_QUERY + "')")
    public CommonResult<PersonalItemRespVO> get(@PathVariable("id") Long id) {
        return success(personalItemService.getItemRespVO(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获取个人事项分页")
    //@PreAuthorize("@ss.hasPermission('" + PersonalItemConstants.PERMISSION_QUERY + "')")
    public CommonResult<PageResult<PersonalItemRespVO>> page(@Valid PersonalItemPageReqVO reqVO) {
        return success(personalItemService.getItemRespVOPage(reqVO));
    }

    @PostMapping("/{id}/change-status")
    @Operation(summary = "变更个人事项状态")
    //@PreAuthorize("@ss.hasPermission('" + PersonalItemConstants.PERMISSION_STATUS + "')")
    public CommonResult<Boolean> changeStatus(@PathVariable("id") Long id,
                                              @Valid @RequestBody PersonalItemStatusActionReqVO reqVO) {
        personalItemService.changeStatus(id, reqVO);
        return success(true);
    }

    @PostMapping("/{id}/worklogs")
    @Operation(summary = "新增个人事项工作日志")
    //@PreAuthorize("@ss.hasPermission('" + PersonalItemConstants.PERMISSION_UPDATE + "')")
    public CommonResult<Long> createWorklog(@PathVariable("id") Long id,
                                            @Valid @RequestBody TaskWorklogSaveReqVO reqVO) {
        return success(personalItemService.createWorklog(id, reqVO));
    }

    @GetMapping("/{id}/worklogs")
    @Operation(summary = "获取个人事项工作日志分页")
    //@PreAuthorize("@ss.hasPermission('" + PersonalItemConstants.PERMISSION_QUERY + "')")
    public CommonResult<PageResult<TaskWorklogRespVO>> getWorklogPage(@PathVariable("id") Long id,
                                                                      @Valid TaskWorklogPageReqVO reqVO) {
        return success(personalItemService.getWorklogPage(id, reqVO));
    }

    @PutMapping("/{id}/worklogs/{worklogId}")
    @Operation(summary = "修改个人事项工作日志")
    //@PreAuthorize("@ss.hasPermission('" + PersonalItemConstants.PERMISSION_UPDATE + "')")
    public CommonResult<Boolean> updateWorklog(@PathVariable("id") Long id,
                                               @PathVariable("worklogId") Long worklogId,
                                               @Valid @RequestBody TaskWorklogSaveReqVO reqVO) {
        personalItemService.updateWorklog(id, worklogId, reqVO);
        return success(true);
    }

    @DeleteMapping("/{id}/worklogs/{worklogId}")
    @Operation(summary = "删除个人事项工作日志")
    //@PreAuthorize("@ss.hasPermission('" + PersonalItemConstants.PERMISSION_UPDATE + "')")
    public CommonResult<Boolean> deleteWorklog(@PathVariable("id") Long id,
                                               @PathVariable("worklogId") Long worklogId) {
        personalItemService.deleteWorklog(id, worklogId);
        return success(true);
    }

    @DeleteMapping("/{id}/worklogs/delete-list")
    @Operation(summary = "批量删除个人事项工作日志")
    //@PreAuthorize("@ss.hasPermission('" + PersonalItemConstants.PERMISSION_UPDATE + "')")
    public CommonResult<Boolean> deleteWorklogs(@PathVariable("id") Long id,
                                                @RequestParam("ids") List<Long> ids) {
        personalItemService.deleteWorklogs(id, ids);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除个人事项")
    //@PreAuthorize("@ss.hasPermission('" + PersonalItemConstants.PERMISSION_DELETE + "')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        personalItemService.deleteItem(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Operation(summary = "批量删除个人事项")
    //@PreAuthorize("@ss.hasPermission('" + PersonalItemConstants.PERMISSION_DELETE + "')")
    public CommonResult<Boolean> deleteList(@RequestParam("ids") List<Long> ids) {
        personalItemService.deleteItems(ids);
        return success(true);
    }

    @PostMapping("/relate-execution")
    @Operation(summary = "批量个人事项关联执行")
    //@PreAuthorize("@ss.hasPermission('" + PersonalItemConstants.PERMISSION_UPDATE + "')")
    public CommonResult<Boolean> relateExecution(@RequestParam("itemIds") List<Long> itemIds,
                                                 @RequestParam("executionId") Long executionId) {
        personalItemService.relateExecution(itemIds, executionId);
        return success(true);
    }

    @GetMapping("/owner/all-execution")
    @Operation(summary = "获取当前登录用户负责的所有执行")
    public CommonResult<List<ProjectExecutionRespVO>> getCurrentUserExecutionList() {
        return success(projectExecutionService.getCurrentUserExecutionList());
    }
}
