// tools/generate-docs/bounded-context-map-generator.ts — Bounded Context Map generation (SPEC-033)
// Generates BOUNDED-CONTEXT-MAP.md from DDD detection results.
import { detectDdd } from '../../engine/architecture-pattern-detector.js';
/** Maps relation type to a human-readable label */
function relationTypeLabel(relationType) {
    const labels = {
        'upstream-downstream': 'Upstream → Downstream',
        acl: 'ACL (Anti-Corruption Layer)',
        'published-language': 'Published Language',
        'shared-kernel': 'Shared Kernel',
        conformist: 'Conformist',
        partnership: 'Partnership (bidirectional)',
    };
    return labels[relationType];
}
/** Generates the BOUNDED-CONTEXT-MAP.md content from detected DDD config */
export async function generateBoundedContextMap(projectPath, knowledge) {
    const dddConfig = await detectDdd(projectPath);
    if (!dddConfig.detected) {
        return ('## Bounded Context Map\n\n' +
            '_No DDD indicators detected in this project. ' +
            'To enable this document, ensure your project has:\n' +
            '- At least 3 DDD tactical suffixes (Aggregate, ValueObject, DomainEvent, Repository, Specification)\n' +
            '- A bounded-contexts/ or contexts/ directory structure\n' +
            '- An ubiquitous-language.md file or EventStorming links in README_\n');
    }
    const contexts = dddConfig.boundedContexts;
    const relations = dddConfig.contextMap?.relations ?? [];
    const updatedAt = new Date().toISOString();
    const lines = [
        '# Bounded Context Map',
        '',
        `> Generated by Planu on ${updatedAt.split('T')[0] ?? updatedAt}`,
        '',
    ];
    // Project info
    lines.push('## Project');
    lines.push('');
    lines.push(`- **Language:** ${knowledge.language}`);
    lines.push(`- **Architecture primary:** ${knowledge.architecture.primary}`);
    lines.push('');
    // Bounded contexts list
    lines.push('## Bounded Contexts');
    lines.push('');
    if (contexts.length === 0) {
        lines.push('_No bounded contexts detected from directory structure._');
    }
    else {
        for (const ctx of contexts) {
            lines.push(`### ${ctx}`);
            lines.push('');
            if (dddConfig.hasUbiquitousLanguage && dddConfig.ubiquitousLanguagePath) {
                lines.push(`- **Ubiquitous Language:** See \`${dddConfig.ubiquitousLanguagePath}\``);
            }
            lines.push('');
        }
    }
    // Context Relations
    lines.push('## Context Relations');
    lines.push('');
    if (relations.length === 0) {
        lines.push('_No inter-context relations detected automatically._');
        lines.push('');
        lines.push('To document relations, add entries to your `context-map.md` or update this file manually.');
        lines.push('');
        lines.push('Relation types to consider:');
        lines.push('- **Upstream → Downstream**: One context depends on data from another');
        lines.push('- **ACL**: Downstream translates upstream model to protect its own domain');
        lines.push('- **Published Language**: Formal contract (OpenAPI, AsyncAPI, Avro)');
        lines.push('- **Shared Kernel**: Code shared between two contexts with explicit coordination');
        lines.push('- **Conformist**: Downstream adopts upstream model without translation');
        lines.push('- **Partnership**: Bidirectional coordination for mutual changes');
    }
    else {
        lines.push('| Upstream | Downstream | Relation Type | Integration Mechanism |');
        lines.push('|----------|------------|---------------|-----------------------|');
        for (const rel of relations) {
            const mechanism = rel.integrationMechanism ?? '_not specified_';
            lines.push(`| ${rel.upstream} | ${rel.downstream} | ${relationTypeLabel(rel.relationType)} | ${mechanism} |`);
        }
    }
    lines.push('');
    // EventStorming
    if (dddConfig.hasEventStormingDiagrams) {
        lines.push('## EventStorming');
        lines.push('');
        lines.push('EventStorming diagrams detected in README. See project documentation for links to Miro/Mural boards.');
        lines.push('');
    }
    // Metadata
    lines.push('---');
    lines.push('');
    lines.push('## Detection Metadata');
    lines.push('');
    lines.push(`- **DDD detected:** ${String(dddConfig.detected)}`);
    lines.push(`- **Bounded contexts found:** ${contexts.length}`);
    lines.push(`- **Has ubiquitous language:** ${String(dddConfig.hasUbiquitousLanguage)}`);
    lines.push(`- **Has EventStorming diagrams:** ${String(dddConfig.hasEventStormingDiagrams)}`);
    lines.push(`- **Last updated:** ${updatedAt}`);
    lines.push('');
    lines.push('_Note: This map is auto-generated from code structure. Manual review is recommended._');
    return lines.join('\n');
}
//# sourceMappingURL=bounded-context-map-generator.js.map