package cd.casic.ci.api;

import cd.casic.ci.process.dto.req.sast.*;
import cd.casic.ci.process.dto.resp.sast.*;
import cd.casic.ci.process.process.service.sast.SastService;
import cd.casic.framework.commons.exception.enums.GlobalErrorCodeConstants;
import cd.casic.framework.commons.pojo.CommonResult;
import cd.casic.framework.commons.pojo.PageParam;
import cd.casic.framework.commons.pojo.PageResult;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.Negative;
import jakarta.ws.rs.Path;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/sast")
public class SastController {
    @Resource
    private SastService sastService;
    /**
     * sast判断应用名称是否存在
     * */
    @GetMapping("/applicationExist")
    public CommonResult<Boolean> applicationExist(@RequestParam String applicationName){
        Boolean b = sastService.applicationExist(applicationName);
        return CommonResult.success(b);
    }
    @GetMapping("/getToken")
    public CommonResult<String> getToken(){
        return CommonResult.success(sastService.getToken());
    }
    /**
     * sast上传检测文件
     * */
    @PostMapping("/uploadFile")
    public CommonResult<SastFileUploadResp> uploadFile(MultipartFile multipartFile){
        File tempFile = null;
        try {
            byte[] bytes = multipartFile.getBytes();
            String[] split = multipartFile.getOriginalFilename().split("\\.");
            tempFile = File.createTempFile(split[0], "."+split[1]);
            FileOutputStream fos = new FileOutputStream(tempFile);
            fos.write(bytes);
            return CommonResult.success(sastService.uploadFile(tempFile));
        } catch (IOException e) {
            return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR);
        }finally {
            if (tempFile!=null) {
                tempFile.deleteOnExit();
            }
        }
    }
    /**
     * sast创建语言检测任务
     * */
    @PostMapping("/createIdentifiedLanguageTask")
    public CommonResult<SastIdentifiedLanguageTaskResp> createIdentifiedLanguageTask(@RequestBody SastIdentifiedLanguageTaskReq req){
        SastIdentifiedLanguageTaskResp identifiedLanguageTask = sastService.createIdentifiedLanguageTask(req);
        return CommonResult.success(identifiedLanguageTask);
    }
    /**
     * sast 获取检测任务状态
     * */
    @GetMapping("/getIdentifiedLanguageTaskStatus/{taskId}")
    public CommonResult<SastIdentifiedLanguageTaskResp> getIdentifiedLanguageTaskStatus(@PathVariable String taskId){
        SastIdentifiedLanguageTaskResp identifiedLanguageTaskStatus = sastService.getIdentifiedLanguageTaskStatus(taskId);
        return CommonResult.success(identifiedLanguageTaskStatus);
    }
    /**
     * sast 获取引擎配置
     * */
    @PostMapping("/engineConfig")
    public CommonResult<List<SastEngineConfigResp>> engineConfig(@RequestBody SastEngineConfigReq req){
        return CommonResult.success(sastService.engineConfig(req.getLanguageIdList()));
    }
    /**
     * sast 获取语言模板等信息
     * */
    @GetMapping("/detectionConfig/{languageId}")
    public CommonResult<SastDetectionConfigResp> detectionConfig(@PathVariable String languageId){
        SastDetectionConfigResp sastDetectionConfigResp = sastService.detectionConfig(languageId);
        return CommonResult.success(sastDetectionConfigResp);
    }
    /**
     * sast上传 应用创建或者更新
     * */
    @PostMapping("/applicationCreateOrUpdate")
    public CommonResult<SastApplicationCreateResp> applicationCreateOrUpdate(@RequestBody SastApplicationStashReq req){
        SastApplicationCreateResp sastApplicationCreateResp = sastService.applicationStash(req);
        return CommonResult.success(sastApplicationCreateResp);
    }
    /**
     * sast 应用信息回显
     * */
    @GetMapping("/getApplicationEcho/{applicationId}")
    public CommonResult<SastApplicationEchoResp> getApplicationEcho(@PathVariable String applicationId){
        SastApplicationEchoResp applicationEcho = sastService.getApplicationEcho(applicationId);
        return CommonResult.success(applicationEcho);
    }
    /**
     * sast 导出报告
     * */
    @PostMapping("/reportIndex")
    public CommonResult<String> reportIndex(@RequestBody SastReportCreateReq req){
        String reportId = sastService.reportIndex(req);
        return CommonResult.success(reportId);
    }
    /**
     * sast 下载报告
     * */
    @GetMapping("/reportDownload")
    public CommonResult<String> reportDownLoad(@RequestParam String reportId){
        return CommonResult.success(sastService.reportDownload(reportId));
    }
    /**
     * sast 获取全部语言信息
     * */
    @GetMapping("/language")
    public CommonResult<List<SastLanguageResp>> language(){
        return CommonResult.success(sastService.language());
    }
    /**
     * sast报告列表
     * */
    @PostMapping("/reportList")
    public CommonResult<PageResult<SastReportResp>> reportList(@RequestBody PageParam pageParam){
        return CommonResult.success(sastService.reportList(pageParam));
    }
    @DeleteMapping("/reportDelete")
    public CommonResult<Void> reportDelete(@RequestParam String reportId){
        sastService.reportDelete(reportId);
        return CommonResult.success();
    }
    @GetMapping("/getApplicationStatus/{applicationId}")
    public CommonResult<SastApplicationStatusResp> getApplicationStatus(@PathVariable String applicationId){
        SastApplicationStatusResp applicationStatus = sastService.getApplicationStatus(applicationId);
        return CommonResult.success(applicationStatus);
    }
    @PostMapping("/applicationBinaryStash")
    CommonResult<SastApplicationBinaryStashResp> applicationBinaryStash(@RequestBody SastApplicationBinaryStashReq req) {
        return CommonResult.success(sastService.applicationBinaryStash(req));
    }
}
