/**
 * Doctor - main diagnostic orchestrator for SpecWeave projects
 */
import { EnvironmentChecker } from './checkers/environment-checker.js';
import { ProjectStructureChecker } from './checkers/project-structure-checker.js';
import { ConfigurationChecker } from './checkers/configuration-checker.js';
import { HooksChecker } from './checkers/hooks-checker.js';
import { PluginsChecker } from './checkers/plugins-checker.js';
import { IncrementsChecker } from './checkers/increments-checker.js';
import { GitChecker } from './checkers/git-checker.js';
import { InstallationHealthChecker } from './checkers/installation-health-checker.js';
import { PluginCurrencyChecker } from './checkers/plugin-currency-checker.js';
import { SkillCurrencyChecker } from './checkers/skill-currency-checker.js';
/**
 * Run all diagnostic checks and return a comprehensive report
 */
export async function runDoctor(projectRoot, options = {}) {
    const checkers = [
        new EnvironmentChecker(),
        new ProjectStructureChecker(),
        new ConfigurationChecker(),
        new HooksChecker(),
        new PluginsChecker(),
        new IncrementsChecker(),
        new GitChecker(),
        new InstallationHealthChecker(),
        new PluginCurrencyChecker(),
        new SkillCurrencyChecker(),
    ];
    const categories = [];
    for (const checker of checkers) {
        try {
            const result = await checker.check(projectRoot, options);
            categories.push(result);
        }
        catch (error) {
            // If a checker fails, record it as a failed category
            categories.push({
                category: checker.category,
                status: 'fail',
                checks: [
                    {
                        name: `${checker.category} check`,
                        status: 'fail',
                        message: `Error: ${error instanceof Error ? error.message : 'unknown error'}`,
                    },
                ],
            });
        }
    }
    // Calculate summary
    const summary = calculateSummary(categories);
    // Only show fix command when not already running with --fix
    let fixCommand;
    if (!options.fix && (summary.warnings > 0 || summary.failures > 0)) {
        fixCommand = determineFix(categories);
    }
    return {
        timestamp: new Date().toISOString(),
        projectRoot,
        categories,
        summary,
        fixCommand,
    };
}
function calculateSummary(categories) {
    let total = 0;
    let passed = 0;
    let warnings = 0;
    let failures = 0;
    let skipped = 0;
    for (const cat of categories) {
        for (const check of cat.checks) {
            total++;
            switch (check.status) {
                case 'pass':
                    passed++;
                    break;
                case 'warn':
                    warnings++;
                    break;
                case 'fail':
                    failures++;
                    break;
                case 'skip':
                    skipped++;
                    break;
            }
        }
    }
    return { total, passed, warnings, failures, skipped };
}
function determineFix(categories) {
    // Check what kind of issues we have
    const hasConfigIssues = categories.some((c) => c.category === 'Configuration' &&
        c.checks.some((ch) => ch.status === 'fail' || ch.status === 'warn'));
    const hasPluginIssues = categories.some((c) => c.category === 'Plugins' &&
        c.checks.some((ch) => ch.status === 'fail' || ch.status === 'warn'));
    const hasHookIssues = categories.some((c) => c.category === 'Hooks' &&
        c.checks.some((ch) => ch.status === 'fail'));
    const hasStructureIssues = categories.some((c) => c.category === 'Project Structure' &&
        c.checks.some((ch) => ch.status === 'fail'));
    const hasInstallationIssues = categories.some((c) => c.category === 'Installation Health' &&
        c.checks.some((ch) => ch.status === 'fail' || ch.status === 'warn'));
    // Prioritize fixes
    if (hasStructureIssues) {
        return 'specweave init';
    }
    if (hasInstallationIssues) {
        return 'specweave doctor --fix';
    }
    if (hasConfigIssues || hasPluginIssues) {
        return 'specweave update';
    }
    if (hasHookIssues) {
        return 'specweave hooks health';
    }
    return 'specweave update';
}
/**
 * Format doctor report for console output
 */
export function formatDoctorReport(report, verbose = false) {
    const lines = [];
    lines.push('');
    lines.push('SpecWeave Doctor');
    lines.push('================');
    lines.push('');
    for (const category of report.categories) {
        const icon = getStatusIcon(category.status);
        lines.push(`${icon} ${category.category}`);
        for (const check of category.checks) {
            const checkIcon = getStatusIcon(check.status);
            const duration = check.durationMs ? ` (${check.durationMs}ms)` : '';
            lines.push(`  ${checkIcon} ${check.name}: ${check.message}${duration}`);
            if (verbose && check.details && check.details.length > 0) {
                for (const detail of check.details) {
                    lines.push(`      ${detail}`);
                }
            }
            if (verbose && check.fixSuggestion) {
                lines.push(`      Fix: ${check.fixSuggestion}`);
            }
        }
        lines.push('');
    }
    // Summary
    const { passed, warnings, failures, skipped } = report.summary;
    lines.push('Summary');
    lines.push('-------');
    lines.push(`${passed} passed, ${warnings} warning(s), ${failures} failure(s), ${skipped} skipped`);
    if (report.fixCommand) {
        lines.push('');
        lines.push(`Fix: ${report.fixCommand}`);
    }
    lines.push('');
    return lines.join('\n');
}
function getStatusIcon(status) {
    switch (status) {
        case 'pass':
            return '\u2713'; // ✓
        case 'warn':
            return '\u26A0'; // ⚠
        case 'fail':
            return '\u2717'; // ✗
        case 'skip':
            return '\u2022'; // •
        default:
            return '?';
    }
}
//# sourceMappingURL=doctor.js.map