"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.exportCommand = void 0;
const drizzle_orm_1 = require("drizzle-orm");
const fs_1 = __importDefault(require("fs"));
const database_1 = require("../database");
const logger_1 = __importDefault(require("../logger"));
const telemetry_1 = __importDefault(require("../telemetry"));
function exportCommand(program) {
    program
        .command('export <evalId>')
        .description('Export an eval record to a JSON file')
        .option('-o, --output [outputPath]', 'Output path for the exported file')
        .action(async (evalId, cmdObj) => {
        try {
            const db = (0, database_1.getDb)();
            let result;
            if (evalId === 'latest') {
                result = await db
                    .select({
                    id: database_1.evals.id,
                    createdAt: database_1.evals.createdAt,
                    description: database_1.evals.description,
                    results: database_1.evals.results,
                    config: database_1.evals.config,
                })
                    .from(database_1.evals)
                    .orderBy((0, drizzle_orm_1.desc)(database_1.evals.createdAt))
                    .limit(1)
                    .execute();
            }
            else {
                result = await db
                    .select({
                    id: database_1.evals.id,
                    createdAt: database_1.evals.createdAt,
                    description: database_1.evals.description,
                    results: database_1.evals.results,
                    config: database_1.evals.config,
                })
                    .from(database_1.evals)
                    .where((0, drizzle_orm_1.eq)(database_1.evals.id, evalId))
                    .execute();
            }
            if (!result || result.length === 0) {
                logger_1.default.error(`No eval found with ID ${evalId}`);
                process.exit(1);
            }
            const jsonData = JSON.stringify(result[0], null, 2);
            if (cmdObj.output) {
                fs_1.default.writeFileSync(cmdObj.output, jsonData);
                logger_1.default.info(`Eval with ID ${evalId} has been successfully exported to ${cmdObj.output}.`);
            }
            else {
                console.log(jsonData);
            }
            telemetry_1.default.record('command_used', {
                name: 'export',
                evalId: evalId,
            });
            await telemetry_1.default.send();
        }
        catch (error) {
            logger_1.default.error(`Failed to export eval: ${error}`);
            process.exit(1);
        }
    });
}
exports.exportCommand = exportCommand;
//# sourceMappingURL=export.js.map