"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const zod_1 = require("zod");
const mablApi_1 = require("@mablhq-internal/runtime/mablApi");
const mcpMablTool_1 = require("./mcpMablTool");
const common_1 = require("../utils/common");
async function processTestRun(testRun, planRun, mablApiClient, workspaceId, server) {
    if (testRun.status === mablApi_1.TestRun.StatusEnum.Failed &&
        testRun.failure_categorization?.is_failure_category_generated !== true) {
        await server?.notification?.({
            method: 'notifications/message',
            params: {
                level: 'info',
                data: `Analyzing test run ${testRun.id}`,
            },
        });
        const analysis = await mablApiClient.analyzeTestRun(workspaceId, {
            test_run_id: testRun.id,
        });
        testRun.failure_categorization = {
            ...testRun.failure_categorization,
            ...analysis,
        };
    }
    return {
        id: testRun.id,
        testId: testRun.journey?.invariant_id ?? '',
        testName: testRun.journey?.name ?? '',
        failureSummary: testRun.failure_summary,
        failureCategorization: testRun.failure_categorization,
        status: testRun.status,
        statusCause: testRun.status_cause,
        outcome: testRun.outcome,
        credentialsId: planRun.credentialsId,
    };
}
const getPlanRunResult = (0, mcpMablTool_1.defineTool)({
    schema: {
        name: 'get_plan_run_result',
        title: 'Get mabl plan run result',
        description: 'Get a mabl plan run result for a given planRunId. Useful for debugging a single plan run. Example: Get mabl plan run result for the last run of the plan "App - Feature: Labels"',
        inputSchema: zod_1.z.object({
            planRunId: zod_1.z
                .string()
                .describe('The id of the mabl plan run to get results for'),
        }),
        type: 'readOnly',
    },
    handle: async (params, context, server) => {
        const { mablApiClient, workspaceId } = context.getCurrentContext();
        const planRunId = params.planRunId;
        const planRun = await mablApiClient.getPlanRun(planRunId);
        const planRunInfo = {
            id: planRun.id,
            status: planRun.status,
            statusCause: planRun.status_cause,
            credentialsId: planRun.run_policy?.credentials_id,
        };
        const testRunsResult = await mablApiClient.getTestRunsForPlan(planRunId);
        const testScriptExecutions = testRunsResult.test_script_executions || [];
        const testRuns = await Promise.all(testScriptExecutions.map((testRun) => processTestRun(testRun, planRunInfo, mablApiClient, workspaceId, server)));
        return {
            content: [
                {
                    type: 'text',
                    text: `Plan run:\n${(0, common_1.jsonPrettyPrint)(planRunInfo)}\nTest runs:\n${(0, common_1.jsonPrettyPrint)(testRuns)}`,
                },
            ],
            structuredContent: {
                planRun: planRunInfo,
                testRuns,
            },
        };
    },
});
exports.default = [getPlanRunResult];
