// tools/generate-docs.ts — Generate documentation
// Uses doc-generator engine: generateDocs()
import { t, ti } from '../i18n/index.js';
import { knowledgeStore, specStore } from '../storage/index.js';
import { generateDocs } from '../engine/doc-generator.js';
import { generateAgentDocumentation } from './generate-docs/agent-docs-generator.js';
import { generateBoundedContextMap } from './generate-docs/bounded-context-map-generator.js';
import { generateMobileInstallationGuide } from './generate-docs/mobile-installation-guide.js';
/** Returns true when any spec targets a native mobile platform. */
function hasMobileSpecs(specs) {
    return specs.some((s) => s.target === 'ios' || s.target === 'android');
}
export async function handleGenerateDocs(args) {
    const { projectId, types: rawTypes, docType, specId, audience = 'developer', format = 'markdown', language = 'en', } = args;
    // docType (singular) overrides types array — convenience shorthand for single-type generation
    const types = docType ? [docType] : rawTypes;
    const knowledge = await knowledgeStore.getKnowledge(projectId);
    if (!knowledge) {
        return {
            content: [
                {
                    type: 'text',
                    text: ti('errors.projectNotFound', { projectId }),
                },
            ],
            isError: true,
        };
    }
    // Load specs for context
    let specs = await specStore.listSpecs(projectId);
    // If a specific specId was given, filter to that spec
    if (specId) {
        const singleSpec = specs.find((s) => s.id === specId);
        if (singleSpec) {
            specs = [singleSpec];
        }
    }
    const result = generateDocs(types, knowledge, specs, audience, language);
    // SPEC-016a: Generate agent documentation for agent projects (criterion 10)
    const agentDocs = generateAgentDocumentation(knowledge, specs);
    // SPEC-033: Generate bounded context map for DDD projects
    const hasBoundedContextMap = types.includes('bounded-context-map');
    const boundedContextMapContent = hasBoundedContextMap
        ? await generateBoundedContextMap(knowledge.projectPath, knowledge)
        : null;
    // Format output
    const lines = [
        `## ${t('tools.generate_docs.description')}`,
        '',
        `**Types:** ${types.join(', ')}`,
        `**Audience:** ${audience}`,
        `**Format:** ${format}`,
        `**Documents generated:** ${result.documents.length}`,
        '',
        `**${result.message}**`,
        '',
    ];
    for (const doc of result.documents) {
        lines.push(`---`);
        lines.push('');
        lines.push(`### ${doc.title}`);
        lines.push('');
        lines.push(`**Type:** ${doc.type} | **Path:** \`${doc.path}\` | **Audience:** ${doc.audience}`);
        lines.push('');
        if (doc.relatedSpecs.length > 0) {
            lines.push(`**Related specs:** ${doc.relatedSpecs.join(', ')}`);
            lines.push('');
        }
        // Show sections summary
        if (doc.sections.length > 0) {
            lines.push('**Sections:**');
            for (const section of doc.sections) {
                const indent = '  '.repeat(section.level - 1);
                lines.push(`${indent}- ${section.title}`);
            }
            lines.push('');
        }
        // Show diagrams if any
        if (doc.diagrams.length > 0) {
            lines.push('**Diagrams:**');
            for (const diagram of doc.diagrams) {
                lines.push(`- ${diagram.title} (${diagram.type})`);
            }
            lines.push('');
        }
        // Show full content
        lines.push('<details>');
        lines.push(`<summary>Full content of ${doc.path}</summary>`);
        lines.push('');
        lines.push(doc.content);
        lines.push('');
        lines.push('</details>');
        lines.push('');
    }
    const content = [
        { type: 'text', text: lines.join('\n') },
    ];
    // SPEC-033: Append bounded context map if requested
    if (boundedContextMapContent) {
        content.push({
            type: 'text',
            text: ['---', '', '## Bounded Context Map (SPEC-033)', '', boundedContextMapContent].join('\n'),
        });
    }
    // SPEC-016a: Append agent documentation sections if agent project
    if (agentDocs) {
        content.push({
            type: 'text',
            text: [
                '---',
                '',
                '## Agent Documentation (SPEC-016a)',
                '',
                agentDocs.usageManual,
                '',
                '---',
                '',
                agentDocs.configurationGuide,
                '',
                '---',
                '',
                agentDocs.troubleshootingGuide,
            ].join('\n'),
        });
    }
    // SPEC-013 (criterion 15): Append mobile installation guide for iOS/Android specs
    const mobileGuide = hasMobileSpecs(specs)
        ? generateMobileInstallationGuide(knowledge, specs, language)
        : null;
    if (mobileGuide) {
        content.push({
            type: 'text',
            text: ['---', '', '## Mobile Installation Guide (SPEC-013)', '', mobileGuide].join('\n'),
        });
    }
    return { content };
}
//# sourceMappingURL=generate-docs.js.map