"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetRelatedDeploymentEventsToolDefinition = exports.getRelatedDeploymentEventsResponseSchema = exports.deploymentInsightsSchema = exports.relatedDeploymentEventInfoSchema = exports.getRelatedDeploymentEventsParamsSchema = void 0;
exports.mapDeploymentStatus = mapDeploymentStatus;
var zod_1 = require("zod");
var toolDefinition_js_1 = require("../toolDefinition.js");
var agentToolNames_js_1 = require("../shared/agentToolNames.js");
/**
 * GetRelatedDeploymentEventsDefinition - Tool definition for fetching related deployment events for comparison.
 */
exports.getRelatedDeploymentEventsParamsSchema = zod_1.z.object({
    deployment_event_id: zod_1.z.string().meta({
        description: 'Required. Deployment event ID.',
    }),
    limit: zod_1.z.number().optional().meta({
        description: 'Maximum number of deployment events to return. Default: 10, Max: 50',
    }),
    filter_status: zod_1.z.enum(['all', 'passed', 'failed']).optional().meta({
        description: 'Filter deployment events by overall status. Default: all',
    }),
    include_current: zod_1.z.boolean().optional().meta({
        description: 'Whether to include the current deployment event in results. Default: false',
    }),
    time_range: zod_1.z.enum(['recent', 'day', 'week', 'month']).optional().meta({
        description: 'Time range to filter deployments. "recent" returns most recent regardless of time, "day" returns from last 24 hours, "week" from last 7 days, "month" from last 30 days. Default: recent',
    }),
});
// Schema for related deployment event info
exports.relatedDeploymentEventInfoSchema = zod_1.z.object({
    deploymentEventId: zod_1.z.string().meta({ description: 'Deployment event ID' }),
    status: zod_1.z.enum(['passed', 'failed', 'running', 'cancelled']).meta({ description: 'Overall status' }),
    eventTime: zod_1.z.string().optional().meta({ description: 'Event time as ISO string' }),
    revision: zod_1.z.string().optional().meta({ description: 'Source code revision' }),
    planRunCounts: zod_1.z
        .object({
        total: zod_1.z.number(),
        passed: zod_1.z.number(),
        failed: zod_1.z.number(),
    })
        .optional()
        .meta({ description: 'Plan run counts for this deployment' }),
    testRunCounts: zod_1.z
        .object({
        total: zod_1.z.number(),
        passed: zod_1.z.number(),
        failed: zod_1.z.number(),
    })
        .optional()
        .meta({ description: 'Test run counts for this deployment' }),
    passRate: zod_1.z.number().optional().meta({ description: 'Pass rate as a percentage (0-100)' }),
});
// Schema for insights
exports.deploymentInsightsSchema = zod_1.z.object({
    totalDeployments: zod_1.z.number().meta({ description: 'Total number of related deployments analyzed' }),
    averagePassRate: zod_1.z.number().optional().meta({ description: 'Average pass rate across all deployments' }),
    consecutivePasses: zod_1.z
        .number()
        .optional()
        .meta({ description: 'Number of consecutive passing deployments before current' }),
    consecutiveFailures: zod_1.z
        .number()
        .optional()
        .meta({ description: 'Number of consecutive failing deployments including current' }),
    lastPassedDeployment: exports.relatedDeploymentEventInfoSchema.optional().meta({
        description: 'Most recent passing deployment event',
    }),
    trend: zod_1.z.enum(['improving', 'declining', 'stable', 'unknown']).optional().meta({
        description: 'Trend based on recent pass rates',
    }),
});
exports.getRelatedDeploymentEventsResponseSchema = zod_1.z.object({
    currentDeployment: exports.relatedDeploymentEventInfoSchema.meta({
        description: 'The current deployment event being analyzed',
    }),
    previousDeployments: zod_1.z.array(exports.relatedDeploymentEventInfoSchema).meta({
        description: 'Historical deployment events for comparison (newest first)',
    }),
    insights: exports.deploymentInsightsSchema.meta({ description: 'Insights from analyzing deployment history' }),
});
exports.GetRelatedDeploymentEventsToolDefinition = (0, toolDefinition_js_1.defineTool)({
    name: agentToolNames_js_1.AgentToolNames.GetRelatedDeploymentEvents,
    description: "Retrieves historical deployment events for comparison and trend analysis.\n\n**Requires deployment_event_id** for the deployment you are comparing from \u2014 never call without it. If you lack an id in workspace scope, use **".concat(agentToolNames_js_1.AgentToolNames.QueryWorkspaceDeploymentEvents, "** first.\n\nUse this tool to:\n- Compare the current deployment with previous deployments\n- Identify trends (improving, declining, stable)\n- Find when failures started occurring\n- Calculate average pass rates across deployments\n- Detect if failures are new or recurring"),
    argumentsSchema: exports.getRelatedDeploymentEventsParamsSchema,
    responseSchema: exports.getRelatedDeploymentEventsResponseSchema,
});
/**
 * Maps API deployment status to normalized status.
 *
 * @param status - The raw status from the API
 * @returns The normalized status
 */
function mapDeploymentStatus(status) {
    switch (status === null || status === void 0 ? void 0 : status.toLowerCase()) {
        case 'succeeded':
        case 'completed':
            return 'passed';
        case 'failed':
            return 'failed';
        case 'cancelled':
        case 'terminated':
            return 'cancelled';
        default:
            return 'running';
    }
}
//# sourceMappingURL=GetRelatedDeploymentEventsDefinition.js.map