package cd.casic.ci.api;


import cd.casic.ci.process.dto.req.pipeline.PipelineCreateReq;
import cd.casic.ci.process.dto.req.pipeline.PipelineQueryReq;
import cd.casic.ci.process.dto.req.pipeline.PipelineReq;
import cd.casic.ci.process.dto.req.pipeline.PipelineUpdateReq;
import cd.casic.ci.process.dto.resp.context.TreeRunContextResp;
import cd.casic.ci.process.dto.resp.pipeline.PipelineFindResp;
import cd.casic.ci.process.engine.executor.PipelineExecutor;
import cd.casic.ci.process.engine.runContext.PipelineRunContext;
import cd.casic.ci.process.enums.PiplineTriggerModeEnum;
import cd.casic.ci.process.process.dataObject.base.BaseIdReq;
import cd.casic.ci.process.process.service.pipeline.PipelineService;
import cd.casic.framework.commons.pojo.CommonResult;
import cd.casic.framework.commons.pojo.PageResult;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName PipelineController
 * @Author hopeli
 * @Date 2025/5/10 10:28
 * @Version 1.0
 */

@RestController
@RequestMapping("/pipeline")
public class PipelineController {

    @Resource
    private PipelineService pipelineService;
    @Resource
    private PipelineExecutor pipelineExecutor;

    @PermitAll
    @PostMapping(path="/createPipeline")
    public CommonResult<String> createPipeline(@RequestBody @Valid PipelineCreateReq pipelineReq){

        String pipelineId = pipelineService.createPipeline(pipelineReq);

        return CommonResult.success(pipelineId);
    }


    @PermitAll
    @PostMapping(path="/findPipelineList")
    public CommonResult<List<PipelineFindResp>> findPipelineList(@RequestBody @Valid PipelineQueryReq pipelineQueryReq){

        List<PipelineFindResp> respList = pipelineService.findPipelineList(pipelineQueryReq);

        return CommonResult.success(respList);
    }

    @PermitAll
    @PostMapping(path="/findPipelineById")
    public CommonResult<PipelineFindResp> findPipelineById(@RequestBody @Valid PipelineQueryReq pipelineQueryReq){

        PipelineFindResp resp = pipelineService.findPipelineById(pipelineQueryReq);

        return CommonResult.success(resp);
    }

    @PostMapping(path="/deletePipeline")
    public CommonResult<Void> deletePipeline(@RequestBody @Valid PipelineReq req){

        pipelineService.deletePipeline(req);

        return CommonResult.success();
    }


    @PostMapping(path="/updatePipeline")
    public CommonResult<Void> updatePipeline(@RequestBody @NotNull @Valid PipelineUpdateReq pipeline){

        pipelineService.updatePipeline(pipeline);

        return CommonResult.success();
    }


    @PostMapping(path="/findPipelinePage")
    public CommonResult<PageResult<PipelineFindResp>> findPipelinePage(@RequestBody @NotNull @Valid PipelineQueryReq query){

        PageResult<PipelineFindResp> pipelineRespPage = pipelineService.findPipelinePage(query);

        return CommonResult.success(pipelineRespPage);
    }




    @PostMapping(path="/pipelineClone")
    public CommonResult<Void> pipelineClone(@RequestBody @Valid PipelineReq req){
        pipelineService.pipelineClone(req);

        return CommonResult.success();
    }
    @Resource
    private HttpServletRequest request;
    @PostMapping("/executePipeline/{pipelineId}")
    public CommonResult<PipelineRunContext> executePipeline(@PathVariable("pipelineId") String pipelineId,@RequestParam(required = false,defaultValue = "false") Boolean fromError){
        PipelineRunContext execute = pipelineExecutor.execute(pipelineId, PiplineTriggerModeEnum.HAND,fromError);
        return CommonResult.success(execute);
    }
    @PostMapping("/getStageRunState/{pipelineId}")
    public CommonResult<TreeRunContextResp> getPipelineRunState(@PathVariable String pipelineId){
        return CommonResult.success(pipelineService.getPipelineRunState(pipelineId));
    }
    @PostMapping("/traversePipelineContext/{pipelineId}")
    public CommonResult<PipelineRunContext> traversePipelineContext(@PathVariable("pipelineId") String pipelineId,@RequestParam(required = false,defaultValue = "false") Boolean fromError){
        pipelineExecutor.traversePipelineContext(pipelineId);
        return CommonResult.success();
    }

    @PostMapping(path="/transformTemplateByPipeline")
    public CommonResult<Void> transformTemplateByPipeline(@RequestBody @Valid BaseIdReq req){

        pipelineService.transformTemplateByPipeline(req);

        return CommonResult.success();
    }
}
