"use strict";
/**
 * Doctor Command Handler
 *
 * Handle doctor command for CCS.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleDoctorCommand = void 0;
const ui_1 = require("../utils/ui");
const logging_1 = require("../services/logging");
const logger = (0, logging_1.createLogger)('commands:doctor');
/**
 * Show help for doctor command
 */
function showHelp() {
    console.log('');
    console.log((0, ui_1.header)('ccs doctor'));
    console.log('');
    console.log('  Run health diagnostics on CCS installation.');
    console.log('');
    console.log((0, ui_1.subheader)('Usage:'));
    console.log(`  ${(0, ui_1.color)('ccs doctor', 'command')} [options]`);
    console.log('');
    console.log((0, ui_1.subheader)('Options:'));
    console.log(`  ${(0, ui_1.color)('--fix, -f', 'command')}     Attempt to auto-fix detected issues`);
    console.log(`  ${(0, ui_1.color)('--help, -h', 'command')}    Show this help message`);
    console.log('');
    console.log((0, ui_1.subheader)('Checks performed:'));
    console.log(`  ${(0, ui_1.dim)('-')} Config files (config.yaml, config.json)`);
    console.log(`  ${(0, ui_1.dim)('-')} CLIProxy installation and process status`);
    console.log(`  ${(0, ui_1.dim)('-')} OAuth port availability (8085, 1455, 51121)`);
    console.log(`  ${(0, ui_1.dim)('-')} Symlink integrity (shared settings)`);
    console.log(`  ${(0, ui_1.dim)('-')} Profile configuration validity`);
    console.log('');
    console.log((0, ui_1.subheader)('Auto-fix capabilities:'));
    console.log(`  ${(0, ui_1.dim)('-')} Kill zombie CLIProxy processes`);
    console.log(`  ${(0, ui_1.dim)('-')} Free blocked OAuth callback ports`);
    console.log(`  ${(0, ui_1.dim)('-')} Regenerate outdated CLIProxy config`);
    console.log(`  ${(0, ui_1.dim)('-')} Restore broken shared settings symlinks`);
    console.log('');
    console.log((0, ui_1.subheader)('Examples:'));
    console.log(`  $ ${(0, ui_1.color)('ccs doctor', 'command')}          ${(0, ui_1.dim)('# Run diagnostics (read-only)')}`);
    console.log(`  $ ${(0, ui_1.color)('ccs doctor --fix', 'command')}    ${(0, ui_1.dim)('# Run diagnostics and fix issues')}`);
    console.log('');
    console.log((0, ui_1.subheader)('Exit codes:'));
    console.log(`  ${(0, ui_1.color)('0', 'command')}  All checks passed`);
    console.log(`  ${(0, ui_1.color)('1', 'command')}  One or more checks failed`);
    console.log('');
}
/**
 * Handle doctor command
 * @param args - Command line arguments
 */
async function handleDoctorCommand(args) {
    await (0, ui_1.initUI)();
    if (args.includes('--help') || args.includes('-h')) {
        showHelp();
        return;
    }
    const shouldFix = args.includes('--fix') || args.includes('-f');
    const startedAt = Date.now();
    logger.stage('intake', 'doctor.start', 'Doctor health check starting', {
        fix: shouldFix,
    });
    const DoctorModule = await Promise.resolve().then(() => __importStar(require('../management/doctor')));
    const Doctor = DoctorModule.default;
    const doctor = new Doctor();
    await doctor.runAllChecks();
    logger.stage('route', 'doctor.checks_complete', 'Doctor checks complete', { healthy: doctor.isHealthy() }, { latencyMs: Date.now() - startedAt });
    if (shouldFix) {
        logger.stage('dispatch', 'doctor.fix_start', 'Attempting auto-fix', {});
        await doctor.fixIssues();
        logger.stage('respond', 'doctor.fix_complete', 'Doctor auto-fix complete', { healthy: doctor.isHealthy() }, { latencyMs: Date.now() - startedAt });
    }
    process.exit(doctor.isHealthy() ? 0 : 1);
}
exports.handleDoctorCommand = handleDoctorCommand;
//# sourceMappingURL=doctor-command.js.map