import { z } from 'zod';
import { config } from '../../config.js';
import { runCommand } from '../../lib/runCommand.js';
import { errorResult, jsonResult, toErrorMessage } from '../shared.js';
import { requireLaravel } from './requireLaravel.js';
export function registerLaravelQueueStatusTool(server) {
    server.registerTool('laravel_queue_status', {
        title: 'Laravel Queue Status',
        description: 'Summarize Laravel queue health via `php artisan queue:failed` (and optionally `queue:batches` when available). Read-only.',
        inputSchema: {
            includeBatches: z
                .boolean()
                .optional()
                .default(false)
                .describe('If true, also run `php artisan queue:batches` when supported.'),
        },
    }, async ({ includeBatches }) => {
        try {
            const laravel = requireLaravel();
            const failed = await runCommand('php', ['artisan', 'queue:failed', '--no-interaction'], {
                cwd: laravel.root,
                timeoutMs: config.defaultCommandTimeoutMs,
            });
            let batches = null;
            if (includeBatches) {
                batches = await runCommand('php', ['artisan', 'queue:batches', '--no-interaction'], {
                    cwd: laravel.root,
                    timeoutMs: config.defaultCommandTimeoutMs,
                });
            }
            const failedLines = failed.output
                .split(/\r?\n/)
                .map((line) => line.trim())
                .filter(Boolean);
            const failedCount = failedLines.filter((line) => /^\d+\s+/.test(line) || /\|/.test(line)).length;
            return jsonResult({
                failed: {
                    exitCode: failed.exitCode,
                    failed: failed.failed,
                    approxRows: failedCount,
                    output: failed.output,
                },
                batches: batches
                    ? {
                        exitCode: batches.exitCode,
                        failed: batches.failed,
                        output: batches.output,
                    }
                    : null,
            });
        }
        catch (error) {
            return errorResult(toErrorMessage(error));
        }
    });
}
//# sourceMappingURL=queueStatus.js.map