// engine/architecture-lint/index.ts — SPEC-961 Architecture Lint Engine
import { readFile } from 'node:fs/promises';
import { glob } from 'glob';
import { checkArchitecture, checkSolid } from '../auditor-architecture.js';
/**
 * Run architecture + SOLID checks on all source files in a project.
 */
export async function runArchitectureLint(input) {
    const { projectPath, exclude = ['node_modules/**', '.git/**', 'dist/**'] } = input;
    const patterns = [
        'src/**/*.{ts,tsx,js,jsx}',
        'app/**/*.{ts,tsx,js,jsx}',
        'pages/**/*.{ts,tsx,js,jsx}',
        'api/**/*.{ts,tsx,js,jsx}',
    ];
    const files = [];
    for (const pattern of patterns) {
        const matches = await glob(pattern, {
            cwd: projectPath,
            absolute: true,
            ignore: exclude,
        });
        files.push(...matches);
    }
    const uniqueFiles = [...new Set(files)];
    const violations = [];
    for (const file of uniqueFiles) {
        let content;
        try {
            content = await readFile(file, 'utf-8');
        }
        catch {
            continue;
        }
        const lines = content.split('\n');
        const ext = file.slice(file.lastIndexOf('.'));
        // Run existing auditor checks
        const solidFindings = checkSolid(file, content, lines, ext);
        const archFindings = checkArchitecture(file, content, lines, projectPath);
        // Convert AuditFinding → ArchitectureViolation
        for (const f of [...solidFindings, ...archFindings]) {
            violations.push({
                rule: f.rule,
                message: f.message,
                filePath: f.file,
                line: 0, // Auditor doesn't track line numbers yet
                severity: f.severity === 'info' ? 'warning' : f.severity,
                suggestion: f.suggestion,
            });
        }
    }
    const errorCount = violations.filter((v) => v.severity === 'error').length;
    const warningCount = violations.filter((v) => v.severity === 'warning').length;
    // Build rule summaries
    const ruleMap = new Map();
    for (const v of violations) {
        const existing = ruleMap.get(v.rule);
        if (existing) {
            existing.count += 1;
            if (v.severity === 'error') {
                existing.severity = 'error';
            }
        }
        else {
            ruleMap.set(v.rule, { count: 1, severity: v.severity });
        }
    }
    const ruleSummaries = Array.from(ruleMap.entries()).map(([rule, { count, severity }]) => ({
        rule,
        count,
        severity,
    }));
    const report = {
        pass: errorCount === 0,
        filesScanned: uniqueFiles.length,
        totalViolations: violations.length,
        errorCount,
        warningCount,
        violations,
        ruleSummaries,
    };
    const humanSummary = buildHumanSummary(report);
    return { report, humanSummary };
}
function buildHumanSummary(report) {
    const lines = [];
    lines.push(`# Architecture Lint Report`);
    lines.push('');
    lines.push(`**Result**: ${report.pass ? 'PASS' : 'FAIL'}`);
    lines.push(`**Files scanned**: ${report.filesScanned}`);
    lines.push(`**Violations**: ${report.totalViolations} (${report.errorCount} errors, ${report.warningCount} warnings)`);
    lines.push('');
    if (report.ruleSummaries.length > 0) {
        lines.push('## Rule Summary');
        lines.push('');
        lines.push('| Rule | Count | Severity |');
        lines.push('|------|-------|----------|');
        for (const rs of report.ruleSummaries) {
            lines.push(`| ${rs.rule} | ${rs.count} | ${rs.severity.toUpperCase()} |`);
        }
        lines.push('');
    }
    const errors = report.violations.filter((v) => v.severity === 'error').slice(0, 5);
    if (errors.length > 0) {
        lines.push('## Top Errors');
        lines.push('');
        for (const e of errors) {
            lines.push(`- **${e.rule}** in \`${e.filePath}\``);
            lines.push(`  - ${e.message}`);
            lines.push(`  - Suggestion: ${e.suggestion}`);
        }
        lines.push('');
    }
    return lines.join('\n');
}
//# sourceMappingURL=index.js.map