"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getApplicationsOutputSchema = void 0;
const zod_1 = require("zod");
const mcpMablTool_1 = require("./mcpMablTool");
const common_1 = require("../utils/common");
exports.getApplicationsOutputSchema = zod_1.z.object({
    applications: zod_1.z.array(zod_1.z.object({
        applicationId: zod_1.z.string(),
        applicationName: zod_1.z.string(),
        environments: zod_1.z.array(zod_1.z.object({
            environmentId: zod_1.z.string(),
            name: zod_1.z.string(),
            url: zod_1.z.string(),
        })),
    })),
});
const getApplications = (0, mcpMablTool_1.defineTool)({
    schema: {
        name: 'get_applications',
        title: 'Get all mabl applications with their environments',
        description: 'Get all mabl applications with their associated environments. Example: Get my mabl applications',
        inputSchema: zod_1.z.object({}),
        outputSchema: exports.getApplicationsOutputSchema,
        type: 'readOnly',
    },
    handle: async (_, context) => {
        const { mablApiClient, workspaceId } = context.getCurrentContext();
        const limit = 500;
        const [applications, deployments, environments] = await Promise.all([
            mablApiClient.getApplications(workspaceId, limit),
            mablApiClient.queryDeploymentEntities(workspaceId),
            mablApiClient.getEnvironments(workspaceId, limit),
        ]);
        const environmentMap = new Map();
        environments.forEach((env) => {
            environmentMap.set(env.id, env);
        });
        const deploymentsByAppId = new Map();
        if (deployments.deployments) {
            deployments.deployments.forEach((deployment) => {
                if (deployment.application_id) {
                    if (!deploymentsByAppId.has(deployment.application_id)) {
                        deploymentsByAppId.set(deployment.application_id, []);
                    }
                    deploymentsByAppId.get(deployment.application_id).push(deployment);
                }
            });
        }
        const applicationsWithEnvironments = applications.map((app) => ({
            applicationId: app.id,
            applicationName: app.name,
            environments: (deploymentsByAppId.get(app.id) || []).map((deployment) => {
                const environment = environmentMap.get(deployment.environment_id);
                return {
                    environmentId: deployment.environment_id,
                    name: environment?.name || 'Unknown Environment',
                    url: deployment.uri || '',
                };
            }),
        }));
        return {
            content: [
                {
                    type: 'text',
                    text: (0, common_1.jsonPrettyPrint)({ applications: applicationsWithEnvironments }),
                },
            ],
            structuredContent: { applications: applicationsWithEnvironments },
        };
    },
});
exports.default = [getApplications];
