// Planu — Ops Document Generators
// Generates ops-type documents: release-notes, runbook, security-doc, database-doc
import { buildSecurityEnvSection } from './builders.js';
import { generateErDiagram } from './diagrams.js';
export function generateReleaseNotes(specs, _language) {
    const sections = [];
    const doneSpecs = specs.filter((s) => s.status === 'done');
    // Group by type
    const features = doneSpecs.filter((s) => s.type === 'feature');
    const bugfixes = doneSpecs.filter((s) => s.type === 'bugfix');
    const refactors = doneSpecs.filter((s) => s.type === 'refactor');
    if (features.length > 0) {
        sections.push({
            title: 'New Features',
            level: 3,
            content: features.map((s) => `- ${s.title}`).join('\n'),
        });
    }
    if (bugfixes.length > 0) {
        sections.push({
            title: 'Bug Fixes',
            level: 3,
            content: bugfixes.map((s) => `- ${s.title}`).join('\n'),
        });
    }
    if (refactors.length > 0) {
        sections.push({
            title: 'Improvements',
            level: 3,
            content: refactors.map((s) => `- ${s.title}`).join('\n'),
        });
    }
    const content = `# Release Notes\n\n## ${new Date().toISOString().split('T')[0]}\n\n` +
        sections.map((s) => `${'#'.repeat(s.level)} ${s.title}\n\n${s.content}`).join('\n\n');
    return {
        type: 'release-notes',
        title: 'Release Notes',
        path: 'docs/release-notes.md',
        content,
        audience: 'stakeholder',
        sections,
        diagrams: [],
        relatedSpecs: doneSpecs.map((s) => s.id),
    };
}
export function generateRunbook(knowledge, _language) {
    const sections = [
        {
            title: 'Service Overview',
            level: 2,
            content: [
                `Language: ${knowledge.language}`,
                `Framework: ${knowledge.framework ?? 'none'}`,
                `Database: ${knowledge.database}`,
                `Deployment: ${knowledge.architecture.deploymentUnits.join(', ')}`,
            ].join('\n'),
        },
        {
            title: 'Health Checks',
            level: 2,
            content: 'Document your health check endpoints and monitoring dashboards here.',
        },
        {
            title: 'Common Issues',
            level: 2,
            content: [
                "### Application won't start",
                '1. Check environment variables are set.',
                '2. Verify database connection.',
                '3. Check logs for error messages.',
                '',
                '### High latency',
                '1. Check database query performance.',
                '2. Review connection pool settings.',
                '3. Check for N+1 queries.',
                '',
                '### Out of memory',
                '1. Check for memory leaks.',
                '2. Review connection/file handle cleanup.',
                '3. Consider increasing memory limits.',
            ].join('\n'),
        },
        {
            title: 'Escalation',
            level: 2,
            content: 'Define your escalation procedures and on-call contacts here.',
        },
    ];
    const content = `# Runbook\n\n` +
        sections.map((s) => `${'#'.repeat(s.level)} ${s.title}\n\n${s.content}`).join('\n\n');
    return {
        type: 'runbook',
        title: 'Runbook',
        path: 'docs/runbook.md',
        content,
        audience: 'ops',
        sections,
        diagrams: [],
        relatedSpecs: [],
    };
}
export function generateSecurityDoc(knowledge, _language) {
    const sections = [
        {
            title: 'Security Overview',
            level: 2,
            content: 'This document outlines the security practices and policies for this project.',
        },
        {
            title: 'Authentication & Authorization',
            level: 2,
            content: 'Document your auth strategy: JWT, OAuth 2.0, API keys, session-based, etc.',
        },
        {
            title: 'Data Protection',
            level: 2,
            content: [
                '- Sensitive data is stored in environment variables, never in code.',
                '- Database connections use TLS in production.',
                '- PII is handled according to GDPR/privacy regulations.',
            ].join('\n'),
        },
        {
            title: 'Environment Variables',
            level: 2,
            content: buildSecurityEnvSection(knowledge),
        },
        {
            title: 'Reporting Vulnerabilities',
            level: 2,
            content: 'If you discover a security vulnerability, please report it responsibly by contacting the maintainers directly.',
        },
    ];
    const content = `# Security Documentation\n\n` +
        sections.map((s) => `${'#'.repeat(s.level)} ${s.title}\n\n${s.content}`).join('\n\n');
    return {
        type: 'security-doc',
        title: 'Security Documentation',
        path: 'docs/security.md',
        content,
        audience: 'developer',
        sections,
        diagrams: [],
        relatedSpecs: [],
    };
}
export function generateDatabaseDoc(knowledge, _language) {
    const sections = [
        {
            title: 'Database Overview',
            level: 2,
            content: knowledge.database !== 'unknown'
                ? `This project uses **${knowledge.database}** as its database engine.`
                : 'No database is configured for this project.',
        },
    ];
    if (knowledge.database !== 'unknown') {
        sections.push({
            title: 'Connection',
            level: 2,
            content: 'Database connection is configured via environment variables. See the Setup Guide for configuration.',
        });
        sections.push({
            title: 'Migrations',
            level: 2,
            content: 'Document your migration strategy here. Include commands for running and reverting migrations.',
        });
        sections.push({
            title: 'Schema',
            level: 2,
            content: 'Add your database schema documentation here, or generate it from your ORM schema.',
        });
    }
    const diagrams = knowledge.database !== 'unknown' ? [generateErDiagram(knowledge)] : [];
    const content = `# Database Documentation\n\n` +
        sections.map((s) => `${'#'.repeat(s.level)} ${s.title}\n\n${s.content}`).join('\n\n');
    return {
        type: 'database-doc',
        title: 'Database Documentation',
        path: 'docs/database.md',
        content,
        audience: 'developer',
        sections,
        diagrams,
        relatedSpecs: [],
    };
}
//# sourceMappingURL=ops-generators.js.map