"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SecurityChartsController = void 0;
const logger_1 = __importDefault(require("../../../logger"));
const securityChartsService_1 = require("../../services/securityChartsService");
const defaultStart = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString();
const defaultEnd = new Date().toISOString();
function toParams(req) {
    const { applicationId, start, end, type, limit, projectId, projectIds } = req.query;
    const projectIdsArr = typeof projectIds === 'string'
        ? projectIds
            .split(',')
            .map((s) => s.trim())
            .filter(Boolean)
        : Array.isArray(projectIds)
            ? projectIds
            : undefined;
    return {
        applicationId: applicationId ?? '',
        start: start || defaultStart,
        end: end || defaultEnd,
        type: type,
        limit: limit != null ? Number(limit) : undefined,
        projectId: projectId,
        projectIds: projectIdsArr,
    };
}
exports.SecurityChartsController = {
    async getSecurityProbes(req, res) {
        try {
            const params = toParams(req);
            const data = await securityChartsService_1.securityChartsService.getSecurityProbes(params);
            res.json({ success: true, data });
        }
        catch (error) {
            logger_1.default.error(`Error in getSecurityProbes: ${error}`);
            res.status(500).json({
                success: false,
                message: error.message,
                error: error.message,
            });
        }
    },
    async getVulnerabilitiesDetected(req, res) {
        try {
            const params = toParams(req);
            const data = await securityChartsService_1.securityChartsService.getVulnerabilitiesDetected(params);
            res.json({ success: true, data });
        }
        catch (error) {
            logger_1.default.error(`Error in getVulnerabilitiesDetected: ${error}`);
            res.status(500).json({
                success: false,
                message: error.message,
                error: error.message,
            });
        }
    },
    async getVulnerabilitiesOverTime(req, res) {
        try {
            const params = toParams(req);
            const data = await securityChartsService_1.securityChartsService.getVulnerabilitiesOverTime(params);
            res.json({ success: true, data });
        }
        catch (error) {
            logger_1.default.error(`Error in getVulnerabilitiesOverTime: ${error}`);
            res.status(500).json({
                success: false,
                message: error.message,
                error: error.message,
            });
        }
    },
    async getCategoryWiseVolumeAnalysis(req, res) {
        try {
            const params = toParams(req);
            const data = await securityChartsService_1.securityChartsService.getCategoryWiseVolumeAnalysis(params);
            res.json({ success: true, data });
        }
        catch (error) {
            logger_1.default.error(`Error in getCategoryWiseVolumeAnalysis: ${error}`);
            res.status(500).json({
                success: false,
                message: error.message,
                error: error.message,
            });
        }
    },
    async getSecurityEvaluationsTrend(req, res) {
        try {
            const params = toParams(req);
            const data = await securityChartsService_1.securityChartsService.getRecentSecurityEvaluations(params);
            res.json({ success: true, data });
        }
        catch (error) {
            logger_1.default.error(`Error in getSecurityEvaluationsTrend: ${error}`);
            res.status(500).json({
                success: false,
                message: error.message,
                error: error.message,
            });
        }
    },
    async getRecentSecurityEvaluations(req, res) {
        try {
            const params = toParams(req);
            const data = await securityChartsService_1.securityChartsService.getRecentSecurityEvaluationsList(params);
            res.json({ success: true, data });
        }
        catch (error) {
            logger_1.default.error(`Error in getRecentSecurityEvaluations: ${error}`);
            res.status(500).json({
                success: false,
                message: error.message,
                error: error.message,
            });
        }
    },
    async getProbeResultsOverTime(req, res) {
        try {
            const params = toParams(req);
            const data = await securityChartsService_1.securityChartsService.getProbeResultsOverTime(params);
            res.json({ success: true, data });
        }
        catch (error) {
            logger_1.default.error(`Error in getProbeResultsOverTime: ${error}`);
            res.status(500).json({
                success: false,
                message: error.message,
                error: error.message,
            });
        }
    },
    async getTopSecurityControlFailures(req, res) {
        try {
            const params = toParams(req);
            const data = await securityChartsService_1.securityChartsService.getTopSecurityControlFailures(params);
            res.json({ success: true, data });
        }
        catch (error) {
            logger_1.default.error(`Error in getTopSecurityControlFailures: ${error}`);
            res.status(500).json({
                success: false,
                message: error.message,
                error: error.message,
            });
        }
    },
};
//# sourceMappingURL=index.js.map