/**
 * Health Reporter
 *
 * Formats and outputs hook health check results.
 * Supports multiple formats: console, markdown, JSON, JUnit XML.
 *
 * Part of increment 0037: Hook Health Check System
 */
import * as fs from '../../utils/fs-native.js';
/**
 * Health Reporter - Formats and outputs results
 */
export class HealthReporter {
    /**
     * Generate report in specified format
     */
    static async generateReport(result, options) {
        const formatters = {
            console: () => this.formatConsoleReport(result, options),
            markdown: () => this.formatMarkdownReport(result, options),
            json: () => this.formatJsonReport(result),
            junit: () => this.formatJUnitReport(result)
        };
        const report = (formatters[options.format] ?? formatters.console)();
        if (options.outputPath) {
            await fs.writeFile(options.outputPath, report, 'utf-8');
        }
        return report;
    }
    /**
     * Format console output with colors
     */
    static formatConsoleReport(result, options) {
        const lines = [];
        const { color, failuresOnly, detailed } = options;
        // Header
        lines.push('');
        lines.push(this.colorize('🏥 Hook Health Check', 'bold', color));
        lines.push(this.colorize('═'.repeat(60), 'dim', color));
        lines.push('');
        // Overall status
        const statusMap = {
            healthy: { icon: '✅', color: 'green' },
            degraded: { icon: '⚠️', color: 'yellow' },
            unhealthy: { icon: '❌', color: 'red' }
        };
        const { icon: statusIcon, color: statusColor } = statusMap[result.overallHealth];
        lines.push(this.colorize(`${statusIcon} ${result.summary.message}`, statusColor, color));
        lines.push('');
        // Hook results
        const resultsToShow = failuresOnly
            ? result.results.filter(r => !r.success)
            : result.results;
        if (resultsToShow.length > 0) {
            lines.push(this.colorize('Hooks Checked:', 'bold', color));
            for (const hookResult of resultsToShow) {
                lines.push(this.formatHookResult(hookResult, detailed, color));
            }
            lines.push('');
        }
        // Summary stats
        lines.push(this.colorize('Summary:', 'bold', color));
        lines.push(`  Total Hooks: ${result.totalHooks}`);
        lines.push(`  ${this.colorize(`✅ Passed: ${result.passedHooks}`, 'green', color)}`);
        if (result.failedHooks > 0) {
            lines.push(`  ${this.colorize(`❌ Failed: ${result.failedHooks}`, 'red', color)}`);
        }
        if (result.criticalFailures > 0) {
            lines.push(`  ${this.colorize(`🚨 Critical: ${result.criticalFailures}`, 'red', color)}`);
        }
        lines.push(`  ⏱️  Total Time: ${result.totalExecutionTime}ms`);
        lines.push('');
        // Recommendations
        if (result.summary.recommendations.length > 0) {
            lines.push(this.colorize('💡 Recommendations:', 'bold', color));
            for (const rec of result.summary.recommendations) {
                lines.push(`  • ${rec}`);
            }
            lines.push('');
        }
        // Performance issues
        if (result.summary.slowHooks.length > 0) {
            lines.push(this.colorize('🐌 Slow Hooks:', 'yellow', color));
            for (const slowHook of result.summary.slowHooks) {
                const hookResult = result.results.find(r => r.hook === slowHook);
                if (hookResult) {
                    lines.push(`  • ${slowHook} (${hookResult.executionTime}ms)`);
                }
            }
            lines.push('');
        }
        // Footer
        lines.push(this.colorize(`Last Check: ${new Date(result.timestamp).toLocaleString()}`, 'dim', color));
        lines.push('');
        return lines.join('\n');
    }
    /**
     * Format single hook result for console
     */
    static formatHookResult(result, detailed, color) {
        const icon = result.success ? '✅' : '❌';
        const hookColor = result.success ? 'green' : 'red';
        const hookLine = `  ${icon} ${result.plugin}/${result.hook} (${result.executionTime}ms)`;
        if (!detailed || result.success) {
            return this.colorize(hookLine, hookColor, color);
        }
        // Detailed error output
        const lines = [this.colorize(hookLine, hookColor, color)];
        for (const error of result.errors) {
            lines.push(`     ${this.colorize('Error:', 'red', color)} ${error.message}`);
            if (error.suggestion) {
                lines.push(`     ${this.colorize('💡 Suggestion:', 'yellow', color)} ${error.suggestion}`);
            }
        }
        for (const warning of result.warnings) {
            lines.push(`     ${this.colorize('⚠️  Warning:', 'yellow', color)} ${warning.message}`);
        }
        return lines.join('\n');
    }
    /**
     * Format markdown report
     */
    static formatMarkdownReport(result, options) {
        const lines = [];
        // Header
        lines.push('# 🏥 Hook Health Check Report');
        lines.push('');
        lines.push(`**Generated**: ${new Date(result.timestamp).toLocaleString()}`);
        lines.push(`**Overall Health**: ${this.healthToEmoji(result.overallHealth)} ${result.overallHealth.toUpperCase()}`);
        lines.push('');
        // Summary
        lines.push('## Summary');
        lines.push('');
        lines.push(`- **Total Hooks**: ${result.totalHooks}`);
        lines.push(`- **Passed**: ✅ ${result.passedHooks}`);
        lines.push(`- **Failed**: ❌ ${result.failedHooks}`);
        lines.push(`- **Critical Failures**: 🚨 ${result.criticalFailures}`);
        lines.push(`- **Total Time**: ${result.totalExecutionTime}ms`);
        lines.push('');
        // Hook Results Table
        lines.push('## Hook Results');
        lines.push('');
        lines.push('| Hook | Plugin | Status | Time (ms) | Errors | Warnings |');
        lines.push('|------|--------|--------|-----------|--------|----------|');
        for (const hookResult of result.results) {
            const status = hookResult.success ? '✅ Pass' : '❌ Fail';
            const errors = hookResult.errors.length;
            const warnings = hookResult.warnings.length;
            lines.push(`| ${hookResult.hook} | ${hookResult.plugin} | ${status} | ${hookResult.executionTime} | ${errors} | ${warnings} |`);
        }
        lines.push('');
        // Failures Detail
        const failures = result.results.filter(r => !r.success);
        if (failures.length > 0) {
            lines.push('## Failures Detail');
            lines.push('');
            for (const failure of failures) {
                lines.push(`### ❌ ${failure.hook}`);
                lines.push('');
                for (const error of failure.errors) {
                    lines.push(`**Error**: ${error.message}`);
                    if (error.suggestion) {
                        lines.push(`💡 **Suggestion**: ${error.suggestion}`);
                    }
                    lines.push('');
                }
            }
        }
        // Recommendations
        if (result.summary.recommendations.length > 0) {
            lines.push('## 💡 Recommendations');
            lines.push('');
            for (const rec of result.summary.recommendations) {
                lines.push(`- ${rec}`);
            }
            lines.push('');
        }
        return lines.join('\n');
    }
    /**
     * Format JSON report
     */
    static formatJsonReport(result) {
        return JSON.stringify(result, null, 2);
    }
    /**
     * Format JUnit XML report (for CI/CD)
     */
    static formatJUnitReport(result) {
        const lines = [];
        lines.push('<?xml version="1.0" encoding="UTF-8"?>');
        lines.push(`<testsuite name="Hook Health Check" tests="${result.totalHooks}" failures="${result.failedHooks}" time="${result.totalExecutionTime / 1000}">`);
        for (const hookResult of result.results) {
            lines.push(`  <testcase name="${hookResult.hook}" classname="${hookResult.plugin}" time="${hookResult.executionTime / 1000}">`);
            if (!hookResult.success) {
                for (const error of hookResult.errors) {
                    lines.push(`    <failure message="${this.escapeXml(error.message)}">`);
                    lines.push(`      ${this.escapeXml(error.suggestion || '')}`);
                    lines.push(`    </failure>`);
                }
            }
            lines.push('  </testcase>');
        }
        lines.push('</testsuite>');
        return lines.join('\n');
    }
    /**
     * Colorize text for console output
     */
    static colorize(text, color, enabled) {
        if (!enabled) {
            return text;
        }
        const colorCode = this.ANSI_CODES[color] ?? '';
        return `${colorCode}${text}${this.ANSI_CODES.reset}`;
    }
    /**
     * Convert health status to emoji
     */
    static healthToEmoji(health) {
        return this.HEALTH_EMOJI[health] ?? '⚪';
    }
    /**
     * Escape XML special characters
     */
    static escapeXml(text) {
        return text
            .replace(/&/g, '&amp;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&apos;');
    }
    /**
     * Create default report options
     */
    static createDefaultOptions(format = 'console') {
        return {
            format,
            detailed: false,
            color: true,
            failuresOnly: false
        };
    }
}
HealthReporter.ANSI_CODES = {
    red: '\x1b[31m',
    green: '\x1b[32m',
    yellow: '\x1b[33m',
    blue: '\x1b[34m',
    bold: '\x1b[1m',
    dim: '\x1b[2m',
    reset: '\x1b[0m'
};
HealthReporter.HEALTH_EMOJI = {
    healthy: '🟢',
    degraded: '🟡',
    unhealthy: '🔴'
};
//# sourceMappingURL=HealthReporter.js.map