"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateTestSuite = exports.createTestRun = void 0;
const logger_1 = __importDefault(require("../../../logger"));
const activityLogService_1 = require("../../services/activityLogService");
const testRunService_1 = require("../../services/testRunService");
const createTestRun = async (req, res) => {
    try {
        const { evaluateOptions, applicationIds, libraryId, testRunId, apiKey, isCustomProvider = false, orgId, projectId, ...testSuite } = req.body;
        const token = req.headers['x-access-token'] || req.headers.authorization;
        const { userId, userName } = (0, activityLogService_1.getUserInfo)(req);
        const result = await testRunService_1.testRunService.createTestRun({
            evaluateOptions,
            applicationIds,
            libraryId,
            testRunId,
            apiKey,
            isCustomProvider,
            orgId,
            projectId,
            testSuite,
            token,
            userId,
            userName,
        });
        // Check if result is an error response
        if ('success' in result && result.success === false) {
            res.status(400).json(result);
            return;
        }
        // Success response
        res.json(result);
    }
    catch (error) {
        logger_1.default.error(`Error while test run: ${error.message}`);
        res.status(500).json({
            success: false,
            message: error.message || 'Error while test run',
        });
    }
};
exports.createTestRun = createTestRun;
const generateTestSuite = async (req, res) => {
    try {
        const { tests, datasetIds, orgId, projectId, parentPromptId } = req.body;
        if (!tests || !Array.isArray(tests)) {
            res.status(400).json({ error: 'tests is required and must be an array' });
            return;
        }
        const token = req.headers['x-access-token'] || req.headers.authorization;
        const apiKey = req.body.apiKey;
        const result = await testRunService_1.testRunService.generateTestSuite({
            tests,
            datasetIds,
            orgId,
            projectId,
            parentPromptId,
            token,
            apiKey,
        });
        res.status(200).json(result);
    }
    catch (error) {
        logger_1.default.error(`Error in generateTestSuite: ${error.message}`);
        res.status(500).json({ error: error.message || 'Internal server error' });
    }
};
exports.generateTestSuite = generateTestSuite;
//# sourceMappingURL=create-test-run.js.map