import { z } from 'zod';
import { defineTool, } from '../toolDefinition.js';
import { AgentToolNames } from '../shared/agentToolNames.js';
/**
 * GetRelatedDeploymentEventsDefinition - Tool definition for fetching related deployment events for comparison.
 */
export const getRelatedDeploymentEventsParamsSchema = z.object({
    deployment_event_id: z.string().meta({
        description: 'Required. Deployment event ID.',
    }),
    limit: z.number().optional().meta({
        description: 'Maximum number of deployment events to return. Default: 10, Max: 50',
    }),
    filter_status: z.enum(['all', 'passed', 'failed']).optional().meta({
        description: 'Filter deployment events by overall status. Default: all',
    }),
    include_current: z.boolean().optional().meta({
        description: 'Whether to include the current deployment event in results. Default: false',
    }),
    time_range: 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
export const relatedDeploymentEventInfoSchema = z.object({
    deploymentEventId: z.string().meta({ description: 'Deployment event ID' }),
    status: z.enum(['passed', 'failed', 'running', 'cancelled']).meta({ description: 'Overall status' }),
    eventTime: z.string().optional().meta({ description: 'Event time as ISO string' }),
    revision: z.string().optional().meta({ description: 'Source code revision' }),
    planRunCounts: z
        .object({
        total: z.number(),
        passed: z.number(),
        failed: z.number(),
    })
        .optional()
        .meta({ description: 'Plan run counts for this deployment' }),
    testRunCounts: z
        .object({
        total: z.number(),
        passed: z.number(),
        failed: z.number(),
    })
        .optional()
        .meta({ description: 'Test run counts for this deployment' }),
    passRate: z.number().optional().meta({ description: 'Pass rate as a percentage (0-100)' }),
});
// Schema for insights
export const deploymentInsightsSchema = z.object({
    totalDeployments: z.number().meta({ description: 'Total number of related deployments analyzed' }),
    averagePassRate: z.number().optional().meta({ description: 'Average pass rate across all deployments' }),
    consecutivePasses: z
        .number()
        .optional()
        .meta({ description: 'Number of consecutive passing deployments before current' }),
    consecutiveFailures: z
        .number()
        .optional()
        .meta({ description: 'Number of consecutive failing deployments including current' }),
    lastPassedDeployment: relatedDeploymentEventInfoSchema.optional().meta({
        description: 'Most recent passing deployment event',
    }),
    trend: z.enum(['improving', 'declining', 'stable', 'unknown']).optional().meta({
        description: 'Trend based on recent pass rates',
    }),
});
export const getRelatedDeploymentEventsResponseSchema = z.object({
    currentDeployment: relatedDeploymentEventInfoSchema.meta({
        description: 'The current deployment event being analyzed',
    }),
    previousDeployments: z.array(relatedDeploymentEventInfoSchema).meta({
        description: 'Historical deployment events for comparison (newest first)',
    }),
    insights: deploymentInsightsSchema.meta({ description: 'Insights from analyzing deployment history' }),
});
export const GetRelatedDeploymentEventsToolDefinition = defineTool({
    name: AgentToolNames.GetRelatedDeploymentEvents,
    description: `Retrieves historical deployment events for comparison and trend analysis.

**Requires deployment_event_id** for the deployment you are comparing from — never call without it. If you lack an id in workspace scope, use **${AgentToolNames.QueryWorkspaceDeploymentEvents}** first.

Use this tool to:
- Compare the current deployment with previous deployments
- Identify trends (improving, declining, stable)
- Find when failures started occurring
- Calculate average pass rates across deployments
- Detect if failures are new or recurring`,
    argumentsSchema: getRelatedDeploymentEventsParamsSchema,
    responseSchema: getRelatedDeploymentEventsResponseSchema,
});
/**
 * Maps API deployment status to normalized status.
 *
 * @param status - The raw status from the API
 * @returns The normalized status
 */
export function mapDeploymentStatus(status) {
    switch (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