/**
 * Doctor Command
 * Diagnostic tool to check CV-Git health and suggest fixes
 */
import { Command } from 'commander';
import chalk from 'chalk';
import { simpleGit } from 'simple-git';
import { promises as fs } from 'fs';
import { getConfig } from '../config.js';
import { exec } from 'child_process';
import { promisify } from 'util';
import { addGlobalOptions } from '../utils/output.js';
const execAsync = promisify(exec);
export function doctorCommand() {
    const cmd = new Command('doctor');
    cmd.description('Run diagnostics and health checks');
    cmd.option('--fix', 'Attempt to fix issues automatically');
    addGlobalOptions(cmd);
    cmd.action(async (options) => {
        try {
            const results = [];
            // Run all diagnostic checks
            results.push(await checkGitInstalled());
            results.push(await checkGitRepository());
            results.push(await checkNodeVersion());
            results.push(await checkPnpmInstalled());
            results.push(await checkCVGitInitialized());
            results.push(await checkConfiguration());
            results.push(await checkCredentials());
            results.push(await checkFalkorDB());
            results.push(await checkQdrant());
            results.push(await checkDiskSpace());
            results.push(await checkNetworkConnectivity());
            if (options.json) {
                console.log(JSON.stringify({ results }, null, 2));
            }
            else {
                console.log(chalk.bold('\n🔍 Running CV-Git Diagnostics...\n'));
                // Display results
                const passed = results.filter(r => r.status === 'pass').length;
                const warned = results.filter(r => r.status === 'warn').length;
                const failed = results.filter(r => r.status === 'fail').length;
                for (const result of results) {
                    let icon;
                    let color;
                    switch (result.status) {
                        case 'pass':
                            icon = '✓';
                            color = chalk.green;
                            break;
                        case 'warn':
                            icon = '⚠';
                            color = chalk.yellow;
                            break;
                        case 'fail':
                            icon = '✗';
                            color = chalk.red;
                            break;
                    }
                    console.log(color(`${icon} ${result.name}`));
                    console.log(`  ${result.message}`);
                    if (result.fix) {
                        console.log(chalk.cyan(`  → Fix: ${result.fix}`));
                    }
                    console.log();
                }
                // Summary
                console.log(chalk.bold('Summary:'));
                console.log(chalk.green(`  ✓ ${passed} passed`));
                if (warned > 0)
                    console.log(chalk.yellow(`  ⚠ ${warned} warnings`));
                if (failed > 0)
                    console.log(chalk.red(`  ✗ ${failed} failed`));
                console.log();
                // Overall status
                if (failed === 0 && warned === 0) {
                    console.log(chalk.green.bold('🎉 All checks passed! CV-Git is healthy.\n'));
                    process.exit(0);
                }
                else if (failed === 0) {
                    console.log(chalk.yellow('⚠️  Some warnings found. CV-Git should work but may have issues.\n'));
                    process.exit(0);
                }
                else {
                    console.log(chalk.red('❌ Some checks failed. Please fix the issues above.\n'));
                    process.exit(1);
                }
            }
        }
        catch (error) {
            console.error(chalk.red('✗ Doctor command failed:'), error.message);
            process.exit(1);
        }
    });
    return cmd;
}
/**
 * Check if git is installed
 */
async function checkGitInstalled() {
    try {
        const { stdout } = await execAsync('git --version');
        return {
            name: 'Git Installation',
            status: 'pass',
            message: stdout.trim(),
        };
    }
    catch {
        return {
            name: 'Git Installation',
            status: 'fail',
            message: 'Git is not installed',
            fix: 'Install git from https://git-scm.com/',
        };
    }
}
/**
 * Check if we're in a git repository
 */
async function checkGitRepository() {
    try {
        const git = simpleGit();
        const isRepo = await git.checkIsRepo();
        if (isRepo) {
            return {
                name: 'Git Repository',
                status: 'pass',
                message: 'Current directory is a git repository',
            };
        }
        else {
            return {
                name: 'Git Repository',
                status: 'fail',
                message: 'Not in a git repository',
                fix: 'Run "git init" or navigate to a git repository',
            };
        }
    }
    catch (error) {
        return {
            name: 'Git Repository',
            status: 'fail',
            message: error.message,
        };
    }
}
/**
 * Check Node.js version
 */
async function checkNodeVersion() {
    const version = process.version;
    const major = parseInt(version.slice(1).split('.')[0]);
    if (major >= 18) {
        return {
            name: 'Node.js Version',
            status: 'pass',
            message: `${version} (>= 18.0.0 required)`,
        };
    }
    else {
        return {
            name: 'Node.js Version',
            status: 'fail',
            message: `${version} (< 18.0.0)`,
            fix: 'Upgrade to Node.js 18 or higher',
        };
    }
}
/**
 * Check if pnpm is installed
 */
async function checkPnpmInstalled() {
    try {
        const { stdout } = await execAsync('pnpm --version');
        return {
            name: 'pnpm Installation',
            status: 'pass',
            message: `v${stdout.trim()}`,
        };
    }
    catch {
        return {
            name: 'pnpm Installation',
            status: 'warn',
            message: 'pnpm not installed',
            fix: 'Install pnpm: npm install -g pnpm',
        };
    }
}
/**
 * Check if CV-Git is initialized
 */
async function checkCVGitInitialized() {
    try {
        await fs.access('.cv');
        return {
            name: 'CV-Git Initialization',
            status: 'pass',
            message: 'Repository is initialized',
        };
    }
    catch {
        return {
            name: 'CV-Git Initialization',
            status: 'warn',
            message: 'Repository not initialized',
            fix: 'Run "cv init" to initialize CV-Git',
        };
    }
}
/**
 * Check configuration
 */
async function checkConfiguration() {
    try {
        const config = getConfig();
        const exists = await config.exists();
        if (exists) {
            const cvConfig = await config.load();
            return {
                name: 'Configuration',
                status: 'pass',
                message: `Configured for ${cvConfig.platform.type} (v${cvConfig.version})`,
            };
        }
        else {
            return {
                name: 'Configuration',
                status: 'warn',
                message: 'No configuration file found (using defaults)',
                fix: 'Run "cv config list" to create configuration',
            };
        }
    }
    catch (error) {
        return {
            name: 'Configuration',
            status: 'fail',
            message: `Configuration error: ${error.message}`,
            fix: 'Run "cv config reset" to reset configuration',
        };
    }
}
/**
 * Check credentials
 */
async function checkCredentials() {
    try {
        const { CredentialManager } = await import('@cv-git/credentials');
        const manager = new CredentialManager();
        await manager.init();
        const creds = await manager.list();
        if (creds.length > 0) {
            return {
                name: 'Credentials',
                status: 'pass',
                message: `${creds.length} credential(s) stored`,
            };
        }
        else {
            return {
                name: 'Credentials',
                status: 'warn',
                message: 'No credentials stored',
                fix: 'Run "cv auth setup" to configure credentials',
            };
        }
    }
    catch (error) {
        return {
            name: 'Credentials',
            status: 'warn',
            message: `Credential check failed: ${error.message}`,
        };
    }
}
/**
 * Check FalkorDB
 */
async function checkFalkorDB() {
    try {
        const config = getConfig();
        const cvConfig = await config.load();
        const { createClient } = await import('redis');
        const client = createClient({ url: cvConfig.graph.url });
        await client.connect();
        await client.ping();
        await client.disconnect();
        return {
            name: 'FalkorDB (Knowledge Graph)',
            status: 'pass',
            message: `Connected to ${cvConfig.graph.url}`,
        };
    }
    catch (error) {
        return {
            name: 'FalkorDB (Knowledge Graph)',
            status: 'warn',
            message: 'Not available',
            fix: 'Start FalkorDB: docker run -d --name falkordb -p 6379:6379 falkordb/falkordb',
        };
    }
}
/**
 * Check Qdrant
 */
async function checkQdrant() {
    try {
        const config = getConfig();
        const cvConfig = await config.load();
        const response = await fetch(`${cvConfig.vector.url}/collections`);
        if (response.ok) {
            return {
                name: 'Qdrant (Vector Search)',
                status: 'pass',
                message: `Connected to ${cvConfig.vector.url}`,
            };
        }
        else {
            throw new Error(`HTTP ${response.status}`);
        }
    }
    catch (error) {
        return {
            name: 'Qdrant (Vector Search)',
            status: 'warn',
            message: 'Not available',
            fix: 'Start Qdrant: docker run -d --name qdrant -p 6333:6333 qdrant/qdrant',
        };
    }
}
/**
 * Check disk space
 */
async function checkDiskSpace() {
    try {
        const { stdout } = await execAsync('df -h . | tail -1');
        const parts = stdout.trim().split(/\s+/);
        const usedPercent = parseInt(parts[4]);
        if (usedPercent < 90) {
            return {
                name: 'Disk Space',
                status: 'pass',
                message: `${parts[4]} used`,
            };
        }
        else {
            return {
                name: 'Disk Space',
                status: 'warn',
                message: `${parts[4]} used (running low)`,
                fix: 'Free up disk space',
            };
        }
    }
    catch {
        return {
            name: 'Disk Space',
            status: 'warn',
            message: 'Could not check disk space',
        };
    }
}
/**
 * Check network connectivity
 */
async function checkNetworkConnectivity() {
    try {
        await fetch('https://www.google.com', { method: 'HEAD' });
        return {
            name: 'Network Connectivity',
            status: 'pass',
            message: 'Internet connection available',
        };
    }
    catch {
        return {
            name: 'Network Connectivity',
            status: 'warn',
            message: 'No internet connection (some features may not work)',
        };
    }
}
//# sourceMappingURL=doctor.js.map