"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HistoryExportCommand = exports.HistoryListCommand = exports.HistoryCommand = void 0;
const commander_1 = require("commander");
const chalk_1 = __importDefault(require("chalk"));
const fs = __importStar(require("fs-extra"));
const ExecutionHistory_1 = require("../services/ExecutionHistory");
exports.HistoryCommand = new commander_1.Command("history")
    .description("View execution history")
    .option("--type <type>", "filter by type (operation, validation, brain-update, etc.)")
    .option("--status <status>", "filter by status (success, failure, rolled-back)")
    .option("--limit <number>", "limit results", "20")
    .option("--json", "output as JSON")
    .action(async (options) => {
    const history = new ExecutionHistory_1.ExecutionHistory();
    const entries = await history.list({
        type: options.type,
        status: options.status,
        limit: parseInt(options.limit) || 20,
    });
    if (options.json) {
        console.log(JSON.stringify(entries, null, 2));
        return;
    }
    if (entries.length === 0) {
        console.log(chalk_1.default.dim("\n  No history entries found\n"));
        return;
    }
    console.log(chalk_1.default.bold(`\n📜 Execution History (last ${entries.length})`));
    console.log(chalk_1.default.dim("─".repeat(60)));
    for (const entry of entries) {
        const icon = entry.status === "success"
            ? chalk_1.default.green("✓")
            : entry.status === "failure"
                ? chalk_1.default.red("✗")
                : chalk_1.default.yellow("↩");
        const typeColor = {
            operation: chalk_1.default.cyan,
            validation: chalk_1.default.magenta,
            "brain-update": chalk_1.default.blue,
            "question-answer": chalk_1.default.yellow,
            "agent-run": chalk_1.default.green,
            pipeline: chalk_1.default.white,
        };
        const color = typeColor[entry.type] || chalk_1.default.dim;
        console.log(`  ${icon} ${color(entry.type.padEnd(16))} ${entry.action.substring(0, 50)}`);
        console.log(`     ${chalk_1.default.dim(entry.id.substring(0, 24))} · ${chalk_1.default.dim(new Date(entry.timestamp).toLocaleString())} · ${entry.duration}ms`);
    }
    console.log();
});
exports.HistoryListCommand = new commander_1.Command("history:list")
    .description("List all history entries with full details")
    .option("--type <type>", "filter by type")
    .option("--status <status>", "filter by status")
    .option("--since <date>", "filter by date (ISO string)")
    .option("--limit <number>", "limit results", "50")
    .option("--json", "output as JSON")
    .action(async (options) => {
    const history = new ExecutionHistory_1.ExecutionHistory();
    const entries = await history.list({
        type: options.type,
        status: options.status,
        since: options.since,
        limit: parseInt(options.limit) || 50,
    });
    if (options.json) {
        console.log(JSON.stringify(entries, null, 2));
        return;
    }
    if (entries.length === 0) {
        console.log(chalk_1.default.dim("\n  No entries found\n"));
        return;
    }
    for (const entry of entries) {
        const statusIcon = entry.status === "success"
            ? chalk_1.default.green("✓")
            : entry.status === "failure"
                ? chalk_1.default.red("✗")
                : chalk_1.default.yellow("↩");
        console.log(`\n  ${statusIcon} ${chalk_1.default.bold(entry.action)}`);
        console.log(`     ${chalk_1.default.dim("ID:")}   ${entry.id}`);
        console.log(`     ${chalk_1.default.dim("Type:")} ${entry.type}`);
        console.log(`     ${chalk_1.default.dim("Time:")} ${new Date(entry.timestamp).toLocaleString()}`);
        console.log(`     ${chalk_1.default.dim("Dur:")}  ${entry.duration}ms`);
    }
    console.log();
});
exports.HistoryExportCommand = new commander_1.Command("history:export")
    .description("Export execution history as JSON or Markdown")
    .option("--format <format>", "export format (json or md)", "json")
    .option("--output <path>", "output file path")
    .action(async (options) => {
    const history = new ExecutionHistory_1.ExecutionHistory();
    const format = options.format === "md" ? "md" : "json";
    const output = await history.export(format);
    if (options.output) {
        await fs.writeFile(options.output, output, "utf-8");
        console.log(chalk_1.default.green(`\n  ✓ Exported to ${options.output}\n`));
    }
    else {
        console.log(output);
    }
});
//# sourceMappingURL=history.js.map