import { InputFile } from "grammy";
import { contextKeyFromCtx } from "../shared/context-key.js";
import { getConnectorHealth, getVersionChecks } from "../../support/operations.js";
import { formatLocalDateTime } from "../shared/bot-rendering.js";
import { createSupportBundle } from "../../support/support-bundle.js";
import { chatBucket } from "./telegram-output.js";
import { telegramRateLimiter } from "./telegram-rate-limit.js";
export function registerTelegramSupportCommands(options) {
    options.bot.command(["support", "diagnostics_bundle"], async (ctx) => {
        if (!ctx.chat) {
            return;
        }
        const health = await getConnectorHealth(cliPathOptions(options.config));
        const versionChecks = await getVersionChecks(cliPathOptions(options.config));
        const bundle = await createSupportBundle({
            config: options.config,
            health,
            versionChecks,
            auditEvents: options.auditLog.list(100),
            agentUpdateJobs: options.agentUpdates.list(),
            source: "telegram",
        });
        const contextKey = contextKeyFromCtx(ctx);
        if (contextKey) {
            options.audit({
                action: "command",
                status: "ok",
                contextKey,
                actor: {
                    channel: "telegram",
                    id: ctx.from?.id !== undefined ? `telegram:${ctx.from.id}` : undefined,
                    label: ctx.from?.username || ctx.from?.first_name || (ctx.from?.id !== undefined ? String(ctx.from.id) : undefined),
                    username: ctx.from?.username,
                    channelUserId: ctx.from?.id !== undefined ? String(ctx.from.id) : undefined,
                },
                actorId: ctx.from?.id,
                actorRole: options.getUserRole(ctx),
                description: "export diagnostics bundle",
                detail: bundle.path,
            });
        }
        await telegramRateLimiter.run(chatBucket(ctx.chat.id), "sendDocument", () => ctx.api.sendDocument(ctx.chat.id, new InputFile(bundle.path, bundle.name), {
            caption: [
                "Diagnostics bundle exported.",
                `Created: ${formatLocalDateTime(new Date(bundle.createdAt))}`,
                `Files: ${bundle.includedFiles.length}`,
                `Size: ${bundle.sizeBytes} bytes`,
            ].join("\n"),
            ...(ctx.message?.message_thread_id ? { message_thread_id: ctx.message.message_thread_id } : {}),
        }));
    });
}
function cliPathOptions(config) {
    return {
        piCliPath: config.piCliPath,
        hermesCliPath: config.hermesCliPath,
        openClawCliPath: config.openClawCliPath,
        claudeCodeCliPath: config.claudeCodeCliPath,
    };
}
