// tools/validate-api-contract-handler.ts — Handler for validate_api_contract (SPEC-322)
import { validateOpenApiContract } from '../engine/api-validation/openapi-impl-validator.js';
import { validateGraphQLSchema } from '../engine/api-validation/graphql-schema-validator.js';
// ---------------------------------------------------------------------------
// Format auto-detection
// ---------------------------------------------------------------------------
function detectFormat(contractPath) {
    const lower = contractPath.toLowerCase();
    if (lower.endsWith('.graphql') || lower.endsWith('.gql') || lower.endsWith('.sdl')) {
        return 'graphql';
    }
    return 'openapi';
}
// ---------------------------------------------------------------------------
// Result formatter
// ---------------------------------------------------------------------------
function formatReport(report) {
    const lines = [
        `## API Contract Validation — ${report.contractTitle}`,
        `Format: ${report.contractFormat.toUpperCase()} | Version: ${report.contractVersion}`,
        `Contract endpoints: ${report.totalContractEndpoints} | Implementation endpoints: ${report.totalImplEndpoints}`,
        '',
        `**Summary:** ${report.summary}`,
    ];
    if (report.missingEndpoints.length > 0) {
        lines.push('', `### Missing Implementations (${report.missingEndpoints.length})`);
        for (const f of report.missingEndpoints) {
            lines.push(`- [${f.severity.toUpperCase()}] \`${f.method} ${f.path}\`: ${f.message}`);
        }
    }
    if (report.undocumentedEndpoints.length > 0) {
        lines.push('', `### Undocumented Endpoints (${report.undocumentedEndpoints.length})`);
        for (const f of report.undocumentedEndpoints) {
            lines.push(`- [${f.severity.toUpperCase()}] \`${f.method} ${f.path}\`: ${f.message}`);
        }
    }
    if (report.schemaDrift.length > 0) {
        lines.push('', `### Schema Drift (${report.schemaDrift.length})`);
        for (const f of report.schemaDrift) {
            lines.push(`- [${f.severity.toUpperCase()}] \`${f.path}\`: ${f.message}`);
        }
    }
    if (report.breakingChanges.length > 0) {
        lines.push('', `### Breaking Changes (${report.breakingChanges.length})`);
        for (const f of report.breakingChanges) {
            lines.push(`- [${f.severity.toUpperCase()}] \`${f.path}\`: ${f.message}`);
        }
    }
    const criticalCount = report.findings.filter((f) => f.severity === 'critical').length;
    const warningCount = report.findings.filter((f) => f.severity === 'warning').length;
    lines.push('', `**Total findings:** ${report.findings.length} (${criticalCount} critical, ${warningCount} warnings)`);
    return lines.join('\n');
}
// ---------------------------------------------------------------------------
// Main handler
// ---------------------------------------------------------------------------
export async function handleValidateApiContract(args) {
    const { projectPath, contractPath } = args;
    const format = args.contractFormat === 'auto' || args.contractFormat === undefined
        ? detectFormat(contractPath)
        : args.contractFormat;
    let report;
    if (format === 'graphql') {
        report = await validateGraphQLSchema(contractPath, projectPath);
    }
    else {
        report = await validateOpenApiContract(contractPath, projectPath);
    }
    const text = formatReport(report);
    return {
        content: [{ type: 'text', text }],
    };
}
//# sourceMappingURL=validate-api-contract-handler.js.map