"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSecurityConfigNames = void 0;
const drizzle_orm_1 = require("drizzle-orm");
const database_1 = require("../../../database");
const tables_1 = require("../../../database/tables");
const logger_1 = __importDefault(require("../../../logger"));
const getSecurityConfigNames = async (req, res) => {
    try {
        const { testRunIds } = req.query;
        const db = await (0, database_1.getDb)();
        if (!db) {
            throw new Error('Database connection is required');
        }
        if (!testRunIds || typeof testRunIds !== 'string') {
            res.status(400).json({
                success: false,
                message: 'testRunIds parameter is required as comma-separated string',
            });
            return;
        }
        const testRunIdArray = testRunIds.split(',').map((id) => id.trim());
        // Get security config names for redteam test runs
        const results = await db
            .select({
            testRunId: tables_1.testRunsTable.id,
            securityConfigName: tables_1.securityConfigTable.name,
        })
            .from(tables_1.testRunsTable)
            .leftJoin(tables_1.testRunsToSecurityConfigsTable, (0, drizzle_orm_1.eq)(tables_1.testRunsTable.id, tables_1.testRunsToSecurityConfigsTable.testRunId))
            .leftJoin(tables_1.securityConfigTable, (0, drizzle_orm_1.eq)(tables_1.testRunsToSecurityConfigsTable.securityConfigId, tables_1.securityConfigTable.id))
            .where((0, drizzle_orm_1.and)((0, drizzle_orm_1.inArray)(tables_1.testRunsTable.id, testRunIdArray), (0, drizzle_orm_1.eq)(tables_1.testRunsTable.type, 'redteam')));
        // Create a map of test run ID to security config name
        const securityConfigMap = {};
        results.forEach((result) => {
            securityConfigMap[result.testRunId] = result.securityConfigName;
        });
        res.json({
            success: true,
            data: securityConfigMap,
        });
    }
    catch (error) {
        logger_1.default.error(`Failed to fetch security config names: ${error.message}`);
        res.status(500).json({
            success: false,
            message: error.message || 'Failed to fetch security config names',
        });
    }
};
exports.getSecurityConfigNames = getSecurityConfigNames;
//# sourceMappingURL=get-security-config-names.js.map