// tools/generate-infrastructure.ts — MCP tool handler for generate_infrastructure (SPEC-171)
import { generateInfrastructure } from '../engine/infrastructure/index.js';
import { listSpecs } from '../storage/spec-store.js';
import { compactJson } from './output-formatter.js';
/**
 * Load spec text from storage. Returns empty string if not found.
 */
async function loadSpecText(projectId, specId) {
    try {
        const specs = await listSpecs(projectId);
        const spec = specs.find((s) => s.id === specId);
        if (!spec) {
            return '';
        }
        // Collect all available text fields from the spec object for signal detection
        const parts = [spec.title, spec.id];
        if (Array.isArray(spec.tags)) {
            parts.push(...spec.tags);
        }
        if (Array.isArray(spec.reviewNotes)) {
            parts.push(...spec.reviewNotes);
        }
        return parts.join(' ');
    }
    catch {
        return '';
    }
}
export async function handleGenerateInfrastructure(params) {
    try {
        const specText = await loadSpecText(params.projectId, params.specId);
        if (!specText) {
            return {
                content: [
                    {
                        type: 'text',
                        text: `generate_infrastructure failed: spec "${params.specId}" not found in project "${params.projectId}". ` +
                            'Run list_specs to see available specs, or run init_project first.',
                    },
                ],
                isError: true,
            };
        }
        const result = generateInfrastructure(params, specText);
        // SPEC-174: Version verification notice for infrastructure configs (docker-compose, terraform, k8s)
        const versionNotice = '\n\n> **⚠️ Version Notice:** The image tags and package versions in this configuration are defaults. Always verify against official sources (Docker Hub, Terraform Registry, Helm Hub, GitHub releases) before deploying to production.';
        return {
            content: [
                { type: 'text', text: compactJson(result) },
                { type: 'text', text: versionNotice },
            ],
        };
    }
    catch (error) {
        return {
            content: [
                {
                    type: 'text',
                    text: `generate_infrastructure failed: ${error instanceof Error ? error.message : String(error)}`,
                },
            ],
            isError: true,
        };
    }
}
//# sourceMappingURL=generate-infrastructure.js.map