// Planu — Support Document Generators
// Generates support-type documents: changelog, onboarding, faq, help articles
import { buildPrerequisitesSection, buildProjectStructureSection } from './builders.js';
export function generateChangelog(specs, _language) {
    const sections = [];
    // Group specs by status
    const doneSpecs = specs.filter((s) => s.status === 'done');
    const inProgress = specs.filter((s) => s.status === 'implementing');
    const planned = specs.filter((s) => s.status === 'approved');
    if (doneSpecs.length > 0) {
        sections.push({
            title: 'Completed',
            level: 2,
            content: doneSpecs
                .map((s) => {
                const date = s.actuals?.completedAt ?? s.updatedAt;
                return `- **${s.title}** (${s.type}) — ${date}`;
            })
                .join('\n'),
        });
    }
    if (inProgress.length > 0) {
        sections.push({
            title: 'In Progress',
            level: 2,
            content: inProgress.map((s) => `- **${s.title}** (${s.type})`).join('\n'),
        });
    }
    if (planned.length > 0) {
        sections.push({
            title: 'Planned',
            level: 2,
            content: planned.map((s) => `- **${s.title}** (${s.type})`).join('\n'),
        });
    }
    const content = `# Changelog\n\nAll notable changes tracked via SDD specs.\n\n` +
        sections.map((s) => `${'#'.repeat(s.level)} ${s.title}\n\n${s.content}`).join('\n\n');
    return {
        type: 'changelog',
        title: 'Changelog',
        path: 'CHANGELOG.md',
        content,
        audience: 'developer',
        sections,
        diagrams: [],
        relatedSpecs: specs.map((s) => s.id),
    };
}
/**
 * Estimate "Time to first PR" based on project complexity.
 * Complexity is derived from number of apps, layers, and stack size.
 */
export function estimateTimeToFirstPr(knowledge) {
    const appCount = knowledge.apps.length;
    const layerCount = knowledge.architecture.layers.length;
    const stackSize = knowledge.stack.length;
    const complexity = appCount * 2 + layerCount + Math.floor(stackSize / 3);
    if (complexity <= 4) {
        return '< 2 hours (S project)';
    }
    if (complexity <= 8) {
        return '< 4 hours (M project)';
    }
    if (complexity <= 15) {
        return '< 1 day (L project)';
    }
    return '< 2 days (XL project)';
}
/**
 * Detect if an existing onboarding document is stale by comparing
 * the current project stack with technologies mentioned in the previous content.
 * Returns a list of discrepancy messages (empty if up to date).
 */
export function detectStaleOnboarding(knowledge, previousContent) {
    const discrepancies = [];
    const contentLower = previousContent.toLowerCase();
    for (const tech of knowledge.stack) {
        if (!contentLower.includes(tech.toLowerCase())) {
            discrepancies.push(`New dependency detected: \`${tech}\` — not mentioned in existing onboarding`);
        }
    }
    return discrepancies;
}
function buildOnboardingCoreSections(knowledge) {
    return [
        {
            title: 'Welcome',
            level: 2,
            content: [
                `Welcome to the team! This guide will help you get productive quickly.`,
                '',
                `This is a **${knowledge.language}** project${knowledge.framework ? ` using **${knowledge.framework}**` : ''}.`,
                `Architecture: **${knowledge.architecture.primary}**.`,
            ].join('\n'),
        },
        { title: 'Prerequisites', level: 2, content: buildPrerequisitesSection(knowledge) },
        {
            title: 'First Day Setup',
            level: 2,
            content: [
                '1. Clone the repository.',
                '2. Follow the Setup Guide to install dependencies and configure environment.',
                knowledge.buildCommand ? `3. Run \`${knowledge.buildCommand}\` to verify the build.` : '',
                knowledge.testCommand ? `4. Run \`${knowledge.testCommand}\` to verify tests pass.` : '',
                '5. Read the Architecture Documentation to understand the codebase.',
                '6. Pick a "good first issue" spec to start with.',
            ]
                .filter(Boolean)
                .join('\n'),
        },
        {
            title: 'Key Files and Directories',
            level: 2,
            content: buildProjectStructureSection(knowledge),
        },
        {
            title: 'Development Workflow',
            level: 2,
            content: [
                'We follow Spec Driven Development (SDD):',
                '',
                '1. **Check specs** — Look for assigned specs in the specs directory.',
                '2. **Create branch** — Branch from main with the spec ID.',
                '3. **Implement** — Follow the spec criteria and execution plan.',
                '4. **Validate** — Run validation to check compliance.',
                '5. **Submit PR** — Include spec ID in the PR description.',
            ].join('\n'),
        },
    ];
}
function buildStaleWarningSection(knowledge, previousContent) {
    const discrepancies = detectStaleOnboarding(knowledge, previousContent);
    if (discrepancies.length === 0) {
        return [];
    }
    return [
        {
            title: '⚠️ Stale Content Detected',
            level: 2,
            content: [
                '**This onboarding document may be outdated.** The following dependencies are in the current project but were not mentioned in the previous version:',
                '',
                ...discrepancies.map((d) => `- ${d}`),
                '',
                'Run `generate_docs` with `type: "onboarding"` to regenerate this document with the latest stack.',
            ].join('\n'),
        },
    ];
}
export function generateOnboarding(knowledge, _language, previousContent) {
    const timeEstimate = estimateTimeToFirstPr(knowledge);
    const sections = [
        ...buildOnboardingCoreSections(knowledge),
        {
            title: 'Time to First PR',
            level: 2,
            content: [
                `Estimated time for a new developer to submit their first PR: **${timeEstimate}**`,
                '',
                'This estimate is based on project complexity (apps, layers, stack size).',
                'If actual onboarding consistently takes longer, consider simplifying the setup or improving this guide.',
            ].join('\n'),
        },
        ...(previousContent ? buildStaleWarningSection(knowledge, previousContent) : []),
    ];
    const content = `# Onboarding Guide\n\n` +
        sections.map((s) => `${'#'.repeat(s.level)} ${s.title}\n\n${s.content}`).join('\n\n');
    return {
        type: 'onboarding',
        title: 'Onboarding Guide',
        path: 'docs/onboarding.md',
        content,
        audience: 'developer',
        sections,
        diagrams: [],
        relatedSpecs: [],
    };
}
export function generateFaq(knowledge, _specs, _language) {
    const sections = [];
    const faqs = [
        {
            q: 'What language/framework does this project use?',
            a: `${knowledge.language}${knowledge.framework ? ` with ${knowledge.framework}` : ''}.`,
        },
        {
            q: 'How do I set up the development environment?',
            a: 'Follow the Setup Guide in docs/setup-guide.md.',
        },
        {
            q: 'What database does this project use?',
            a: knowledge.database !== 'unknown'
                ? `We use ${knowledge.database}. See the Database Documentation for details.`
                : 'No database is configured yet.',
        },
        {
            q: 'How do I run tests?',
            a: knowledge.testCommand
                ? `Run \`${knowledge.testCommand}\`.`
                : 'No test command is configured yet.',
        },
        {
            q: 'What is SDD?',
            a: 'Spec Driven Development — a methodology where you write a specification before implementing. Specs live in the specs directory.',
        },
        {
            q: 'How do I create a new feature?',
            a: 'Create a spec first using the SDD MCP tools, then implement following the spec criteria.',
        },
    ];
    sections.push({
        title: 'Frequently Asked Questions',
        level: 2,
        content: faqs.map((faq) => `### ${faq.q}\n\n${faq.a}`).join('\n\n'),
    });
    const content = `# FAQ\n\n` +
        sections.map((s) => `${'#'.repeat(s.level)} ${s.title}\n\n${s.content}`).join('\n\n');
    return {
        type: 'faq',
        title: 'FAQ',
        path: 'docs/faq.md',
        content,
        audience: 'developer',
        sections,
        diagrams: [],
        relatedSpecs: [],
    };
}
export function generateHelpArticles(_knowledge, specs, _language) {
    const sections = [];
    sections.push({
        title: 'Help Center',
        level: 2,
        content: 'Collection of help articles for common tasks and issues.',
    });
    // Generate from completed specs
    for (const spec of specs.filter((s) => s.status === 'done').slice(0, 10)) {
        sections.push({
            title: spec.title,
            level: 3,
            content: `Feature: ${spec.title} (${spec.type})\n\nRefer to spec ${spec.id} for details.`,
        });
    }
    const content = `# Help Articles\n\n` +
        sections.map((s) => `${'#'.repeat(s.level)} ${s.title}\n\n${s.content}`).join('\n\n');
    return {
        type: 'help-articles',
        title: 'Help Articles',
        path: 'docs/help.md',
        content,
        audience: 'end-user',
        sections,
        diagrams: [],
        relatedSpecs: specs.map((s) => s.id),
    };
}
//# sourceMappingURL=support-generators.js.map