import { CallToolRequestSchema } from '@modelcontextprotocol/sdk/types.js';
import { z } from 'zod';
import { analyzeProjectState, generateRecommendations, createBootstrapAnalysis } from './analyzer.js';
import { executeMultipleRecommendations, createBootstrapPlan } from './bootstrapper.js';
const analyzeSchema = z.object({
    projectPath: z.string().optional().describe('Path to the project to analyze (defaults to current directory)')
});
const executeSchema = z.object({
    recommendationIds: z.array(z.string()).describe('IDs of recommendations to execute'),
    projectPath: z.string().optional().describe('Path to the project (defaults to current directory)')
});
const planSchema = z.object({
    priority: z.enum(['high', 'medium', 'low', 'all']).optional().describe('Filter by priority level'),
    projectPath: z.string().optional().describe('Path to the project (defaults to current directory)')
});
export async function setupBootstrapModule(server) {
    // Store current analysis for reference
    let currentAnalysis = [];
    server.setRequestHandler(CallToolRequestSchema, async (request) => {
        const { name, arguments: args } = request.params;
        if (name === 'bootstrap_analyze') {
            const validatedArgs = analyzeSchema.parse(args || {});
            const projectPath = validatedArgs.projectPath || process.cwd();
            try {
                const projectState = await analyzeProjectState(projectPath);
                const recommendations = generateRecommendations(projectState);
                const analysis = createBootstrapAnalysis(projectState, recommendations);
                // Store for later execution
                currentAnalysis = recommendations;
                return {
                    content: [{
                            type: 'text',
                            text: JSON.stringify(analysis, null, 2)
                        }]
                };
            }
            catch (error) {
                throw new Error(`Failed to analyze project: ${error instanceof Error ? error.message : String(error)}`);
            }
        }
        if (name === 'bootstrap_plan') {
            const validatedArgs = planSchema.parse(args || {});
            const projectPath = validatedArgs.projectPath || process.cwd();
            try {
                const projectState = await analyzeProjectState(projectPath);
                let recommendations = generateRecommendations(projectState);
                // Filter by priority if specified
                if (validatedArgs.priority && validatedArgs.priority !== 'all') {
                    recommendations = recommendations.filter(r => r.priority === validatedArgs.priority);
                }
                const plan = createBootstrapPlan(recommendations);
                currentAnalysis = recommendations;
                return {
                    content: [{
                            type: 'text',
                            text: JSON.stringify(plan, null, 2)
                        }]
                };
            }
            catch (error) {
                throw new Error(`Failed to create bootstrap plan: ${error instanceof Error ? error.message : String(error)}`);
            }
        }
        if (name === 'bootstrap_execute') {
            const validatedArgs = executeSchema.parse(args || {});
            const projectPath = validatedArgs.projectPath || process.cwd();
            try {
                // Filter recommendations by provided IDs
                const toExecute = currentAnalysis.filter(r => validatedArgs.recommendationIds.includes(r.id));
                if (toExecute.length === 0) {
                    throw new Error('No valid recommendations found to execute');
                }
                const results = await executeMultipleRecommendations(toExecute, projectPath);
                return {
                    content: [{
                            type: 'text',
                            text: JSON.stringify(results, null, 2)
                        }]
                };
            }
            catch (error) {
                throw new Error(`Failed to execute bootstrap: ${error instanceof Error ? error.message : String(error)}`);
            }
        }
        throw new Error(`Unknown tool: ${name}`);
    });
    return {
        tools: [
            {
                name: 'bootstrap_analyze',
                description: 'Analyze current project state and get recommendations for setup',
                inputSchema: {
                    type: 'object',
                    properties: {
                        projectPath: {
                            type: 'string',
                            description: 'Path to the project to analyze (defaults to current directory)'
                        }
                    },
                    required: []
                }
            },
            {
                name: 'bootstrap_plan',
                description: 'Create a bootstrap plan with prioritized recommendations',
                inputSchema: {
                    type: 'object',
                    properties: {
                        priority: {
                            type: 'string',
                            enum: ['high', 'medium', 'low', 'all'],
                            description: 'Filter by priority level'
                        },
                        projectPath: {
                            type: 'string',
                            description: 'Path to the project (defaults to current directory)'
                        }
                    },
                    required: []
                }
            },
            {
                name: 'bootstrap_execute',
                description: 'Execute selected bootstrap recommendations',
                inputSchema: {
                    type: 'object',
                    properties: {
                        recommendationIds: {
                            type: 'array',
                            items: {
                                type: 'string'
                            },
                            description: 'IDs of recommendations to execute'
                        },
                        projectPath: {
                            type: 'string',
                            description: 'Path to the project (defaults to current directory)'
                        }
                    },
                    required: ['recommendationIds']
                }
            }
        ],
        resources: []
    };
}
//# sourceMappingURL=index.js.map