"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.operationHealthRules = void 0;
function diag(rule, filePath, message, opts = {}) {
    return {
        ruleId: rule.id,
        filePath,
        severity: opts.severity || rule.severity,
        message,
        help: opts.help || rule.help,
        autoFixable: opts.autoFixable ?? false,
    };
}
const opHistoryDirExists = {
    id: "op/history-dir-exists",
    name: "Operation history directory exists",
    category: "node",
    severity: "warning",
    description: "Checks that .mycontext/operations/ directory exists for rollback support",
    help: "Operation history is auto-created when operations are executed. Run any `op:*` command to initialize it.",
    appliesTo: ["node", "nextjs", "turbo"],
    async check(ctx) {
        const results = [];
        const opDir = ".mycontext/operations";
        const exists = await ctx.fileExists(opDir);
        if (!exists) {
            results.push(diag(this, opDir, "Operation history directory not found", {
                severity: "warning",
                autoFixable: true,
                help: "Operation history is auto-created. Run `mycontext op:create-file` or similar to initialize.",
            }));
        }
        return results;
    },
};
const pendingOperations = {
    id: "op/pending-operations",
    name: "No pending operations in history",
    category: "node",
    severity: "warning",
    description: "Checks for any operations with 'pending' status that should be applied or rolled back",
    help: "Use `mycontext op:rollback <id>` to revert pending/failed operations, or re-apply them",
    appliesTo: ["node", "nextjs", "turbo"],
    async check(ctx) {
        const results = [];
        const opDir = ".mycontext/operations";
        const exists = await ctx.fileExists(opDir);
        if (!exists)
            return results;
        const files = await ctx.findFiles(/\.json$/);
        const opFiles = files.filter((f) => f.startsWith(".mycontext/operations/"));
        for (const file of opFiles) {
            const record = await ctx.readJson(file);
            if (record?.operation?.status === "pending") {
                results.push(diag(this, file, `Pending operation: ${record.operation.description || record.operation.id}`, {
                    severity: "warning",
                    autoFixable: true,
                    help: `Run \`mycontext op:rollback ${record.operation.id}\` to clean up, or apply the operation`,
                }));
            }
            if (record?.operation?.status === "failed") {
                results.push(diag(this, file, `Failed operation: ${record.operation.description || record.operation.id}`, {
                    severity: "error",
                    autoFixable: true,
                    help: `Run \`mycontext op:rollback ${record.operation.id}\` to revert any partial changes`,
                }));
            }
        }
        return results;
    },
};
exports.operationHealthRules = [
    opHistoryDirExists,
    pendingOperations,
];
//# sourceMappingURL=operation-health-rules.js.map