package cd.casic.ci.api;


import cd.casic.ci.process.dto.req.report.ReportAssetTaskReq;
import cd.casic.ci.process.dto.req.report.ReportDeleteReq;
import cd.casic.ci.process.dto.req.report.ReportInfoQueryReq;
import cd.casic.ci.process.dto.resp.report.*;
import cd.casic.ci.process.process.dataObject.base.BaseIdPageReq;
import cd.casic.ci.process.process.dataObject.base.BaseIdReq;
import cd.casic.ci.process.process.service.report.ReportService;
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.validation.Valid;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

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

@RestController
@RequestMapping("/report")
public class ReportController {
    @Resource
    private ReportService reportService;

    @PermitAll
    @PostMapping(path="/findReportList")
    public CommonResult<PageResult<ReportResp>> findReportList(@RequestBody @Valid BaseIdPageReq req) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {

        PageResult<ReportResp> respPage = reportService.findReportList(req);

        return CommonResult.success(respPage);
    }

    @PostMapping(path="/deleteReport")
    public CommonResult<ReportResp> deleteReport(@RequestBody @Valid ReportDeleteReq req) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {

        ReportResp reportResp = reportService.deleteReport(req);

        return CommonResult.success(reportResp);
    }


    @PostMapping(path="/downLoadReport")
    public CommonResult<ResponseEntity<String>> downLoadReport(@RequestBody @NotNull @Valid BaseIdReq req) throws Exception {

        ResponseEntity<String> fileInputStream = reportService.downLoadReport(req);

        return  CommonResult.success(fileInputStream);
    }


    @PostMapping(path="/assetReportTask")
    public CommonResult<ReportResp> assetReportTask(@RequestBody @Valid ReportAssetTaskReq req) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {

        ReportResp reportResp = reportService.assetReportTask(req);

        return CommonResult.success(reportResp);
    }


    @PostMapping(path = "/findByScaTaskId")
    public CommonResult<ReportTaskResp> findByScaTaskId(@RequestBody @Valid BaseIdReq req) throws Exception {
        ReportTaskResp taskResp = reportService.findByScaTaskId(req.getTaskId());
        return CommonResult.success(taskResp);
    }


    @PostMapping(path = "/createVulListByScaTaskId")
    public CommonResult<Void> createVulListByScaTaskId(@RequestBody @Valid BaseIdReq req) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        reportService.createVulListByScaTaskId(req);
        return CommonResult.success();
    }


    @PostMapping(path = "/findInfoConsoleSum")
    public CommonResult<ReportInfoConsoleSumResp> findInfoConsoleSum(){
        ReportInfoConsoleSumResp resp = reportService.findInfoConsoleSum();
        return CommonResult.success(resp);
    }

    @PostMapping(path = "/findInfoConsole")
    public CommonResult<List<ReportInfoConsoleResp>> findInfoConsole(@RequestBody @Valid ReportInfoQueryReq query){
        List<ReportInfoConsoleResp> resp = reportService.findInfoConsole(query);
        return CommonResult.success(resp);
    }

    @PostMapping(path = "/findReportInfo")
    public CommonResult<PageResult<ReportInfoResp>> findReportInfo(@RequestBody @Valid ReportInfoQueryReq query){
        PageResult<ReportInfoResp> resp = reportService.findReportInfo(query);
        return CommonResult.success(resp);
    }
}
