package cd.casic.ci.api;


import cd.casic.ci.process.dto.req.stage.StageCreateReq;
import cd.casic.ci.process.dto.req.stage.StageUpdateReq;
import cd.casic.ci.process.dto.req.template.TemplateStageCreateReq;
import cd.casic.ci.process.dto.req.template.TemplateStageUpdateReq;
import cd.casic.ci.process.dto.resp.context.SingletonRunContextResp;
import cd.casic.ci.process.dto.resp.stage.StageResp;
import cd.casic.ci.process.dto.resp.template.TemplateStageResp;
import cd.casic.ci.process.process.dataObject.template.TemplateStage;
import cd.casic.ci.process.process.service.stage.StageService;
import cd.casic.ci.process.process.service.template.TemplateStageService;
import cd.casic.framework.commons.pojo.CommonResult;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @pi.protocol: http
 * @pi.groupName: 流水线多阶段控制器
 */
@RestController
@RequestMapping("/templateStage")
public class TemplateStageController {

    @Resource
    private TemplateStageService stageService;

    /**
     * @pi.name:创建流水线阶段及任务
     * @pi.path:/stage/createStage
     * @pi.methodType:post
     * @pi.request-type:json
     * @pi.param: model=stage
     */
    @PostMapping(path="/createStage")
    public CommonResult<String> createStagesOrTask(@RequestBody TemplateStageCreateReq stage){
        String taskId = stageService.createStagesOrTask(stage);
        return CommonResult.success(taskId);
    }

    /**
     * @pi.name:查询流水线阶段信息
     * @pi.path:/stage/finAllStage
     * @pi.methodType:post
     * @pi.request-type: formdata
     * @pi.param: name=pipelineId;dataType=string;value=pipelineId;
     */
    @PermitAll
    @PostMapping(path="/findAllStage/{templateId}")
    public CommonResult<List<TemplateStageResp>> findAllPipelineTaskOrTask(@NotNull @PathVariable String templateId){
        List<TemplateStageResp> stageRespList = stageService.findAllStagesTask(templateId);
        return CommonResult.success(stageRespList);
    }

    /**
     * @pi.name:更新流水线阶段任务名称Or移动流水线阶段
     * @pi.path:/stage/createStage
     * @pi.methodType:post
     * @pi.request-type:json
     * @pi.param: model=stage
     */
    @PostMapping(path="/updateStage")
    public CommonResult<Void> updateStageTask(@RequestBody @Valid @NotNull TemplateStageUpdateReq stage){
        stageService.updateStagesTask(stage);
        return CommonResult.success();
    }

    /**
     * @pi.name:删除流水线阶段及任务
     * @pi.path:/stage/deleteStage
     * @pi.methodType:post
     * @pi.request-type: formdata
     * @pi.param: name=taskId;dataType=string;value=taskId;
     */
    @PostMapping(path="/deleteTasksOrStage/{taskId}")
    public CommonResult<Void> deleteTasksOrStage(@NotNull@PathVariable String taskId){
        stageService.deleteStagesAndTask(taskId);
        return CommonResult.success();
    }
    /**
     *  复制stage节点
     * */
    @PostMapping(path="/copyStage/{stageId}")
    public CommonResult<Void> copyStage(@NotEmpty@PathVariable String stageId){
        stageService.copyStage(stageId);
        return CommonResult.success();
    }
    @PostMapping("/deleteFirstStage/{stageId}")
    public CommonResult<Void> deleteFirstStage(@PathVariable String stageId){
        stageService.deleteFirstStage(stageId);
        return CommonResult.success();
    }
}




























