package cd.casic.ci.api;


import cd.casic.ci.process.dto.req.target.*;
import cd.casic.ci.process.dto.resp.sftpFile.FileBase64DownloadResp;
import cd.casic.ci.process.dto.resp.target.TargetManagerResp;
import cd.casic.ci.process.dto.resp.target.TargetVersionResp;
import cd.casic.ci.process.process.dataObject.base.BaseIdReq;
import cd.casic.ci.process.process.service.target.TargetManagerService;
import cd.casic.ci.process.process.service.target.TargetVersionService;
import cd.casic.ci.process.util.SftpUploadUtil;
import cd.casic.framework.commons.exception.ServiceException;
import cd.casic.framework.commons.exception.enums.GlobalErrorCodeConstants;
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.HttpServletResponse;
import jakarta.validation.Valid;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HopeLi
 * @version v1.0
 * @ClassName TargetController
 * @Date: 2025/5/17 10:15
 * @Description:
 */

@RestController
@RequestMapping("/target")
public class TargetController {

    @Resource
    private TargetManagerService targetManagerService;

    @Resource
    private TargetVersionService targetVersionService;

    @PostMapping(value = "/createTarget")
    @PermitAll
    public CommonResult<Void> createTarget(@RequestBody @Valid TargetManagerCreateReq req) {

        targetManagerService.createTarget(req);
        return CommonResult.success();
    }


    @PostMapping(value = "/createVersion")
    @PermitAll
    public CommonResult<Void> createVersion(@RequestBody @Valid TargetVersionCreateReq req) {

        targetManagerService.createVersion(req);
        return CommonResult.success();
    }


    @PostMapping(value = "/updateVersion")
    @PermitAll
    public CommonResult<Void> updateVersion(@RequestBody @Valid TargetVersionUpdateReq req){

        targetManagerService.updateVersion(req);
        return CommonResult.success();
    }


    @PostMapping(value = "/updateTarget")
    @PermitAll
    public CommonResult<Void> updateTarget(@RequestBody @Valid TargetManagerUpdateReq req) {

        targetManagerService.updateTarget(req);
        return CommonResult.success();
    }


    @PostMapping(value = "/deleteTarget")
    @PermitAll
    public CommonResult<Void> deleteTarget(@RequestBody @Valid BaseIdReq req) {

        targetManagerService.deleteTarget(req);
        return CommonResult.success();
    }


    @PostMapping(value = "/deleteVersion")
    @PermitAll
    public CommonResult<Void> deleteVersion(@RequestBody @Valid BaseIdReq req) {

        targetManagerService.deleteVersion(req);
        return CommonResult.success();
    }


    @PostMapping(path="/findTargetPage")
    @PermitAll
    public CommonResult<PageResult<TargetManagerResp>> findTargetPage(@RequestBody @NotNull @Valid TargetQueryReq query){

        PageResult<TargetManagerResp> respPage = targetManagerService.findTargetPage(query);

        return CommonResult.success(respPage);
    }


    @PostMapping(path="/findTargetList")
    @PermitAll
    public CommonResult<List<TargetManagerResp>> findTargetList(@RequestBody @Valid TargetQueryReq query){

        List<TargetManagerResp> respList = targetManagerService.findTargetList(query);

        return CommonResult.success(respList);
    }


    @PostMapping(path="/selectListByManagerId")
    @PermitAll
    public CommonResult<List<TargetVersionResp>> selectListByManagerId(@RequestBody @Valid BaseIdReq query){

        List<TargetVersionResp> respList = targetVersionService.selectListByManagerId(query.getId());

        return CommonResult.success(respList);
    }


    @PostMapping(path="/fileUpload")
    public CommonResult<String> fileUpload(MultipartFile file) throws SftpUploadUtil.SftpUploadException {
        String filePath = targetManagerService.fileUpload(file);
        return CommonResult.success(filePath);
    }


    @PostMapping(path="/fileDownload")
    public void fileDownload(@RequestBody @Valid BaseIdReq req, HttpServletResponse response){
        targetManagerService.fileDownload(req,response);
    }

    @PostMapping(path="/zipDownload")
    public CommonResult<FileBase64DownloadResp> zipDownload(@RequestBody @Valid BaseIdReq req, HttpServletResponse response){
        return CommonResult.success(targetManagerService.zipDownload(req, response));
    }

    @PostMapping(path = "/uploadFolder")
    public CommonResult<List<String>> uploadFolder(@RequestParam("files") MultipartFile[] files) {
        List<String> fileUrls = new ArrayList<>();

        for (MultipartFile file : files) {
            if (!file.isEmpty()) {
                try {
                    String filePath = targetManagerService.fileUpload(file);
                    fileUrls.add(filePath);
                } catch (Exception e) {
                    throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(),"文件夹上传失败");
                }
            }
        }

        return CommonResult.success(fileUrls);
    }
}
