"use strict";
/**
 * 🏥 FAF Doctor - Diagnose and fix common issues
 * Health check for your .faf setup
 */
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 () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.doctorCommand = doctorCommand;
const colors_1 = require("../fix-once/colors");
const fs_1 = require("fs");
const path = __importStar(require("path"));
const yaml_1 = require("../fix-once/yaml");
const file_utils_1 = require("../utils/file-utils");
const championship_style_1 = require("../utils/championship-style");
const faf_compiler_1 = require("../compiler/faf-compiler");
async function doctorCommand() {
    console.log();
    console.log(championship_style_1.FAF_COLORS.fafCyan(`🏥 FAF Doctor - Health Check`));
    console.log(championship_style_1.FAF_COLORS.fafWhite('━'.repeat(50)));
    console.log();
    const results = [];
    // Check 1: FAF CLI Version
    console.log(colors_1.chalk.gray('Checking FAF CLI version...'));
    const packageJson = require('../../package.json');
    const version = packageJson.version;
    results.push({
        status: 'ok',
        message: `FAF CLI version: ${version}`
    });
    // Check 2: .faf file exists
    console.log(colors_1.chalk.gray('Checking for .faf file...'));
    const fafPath = await (0, file_utils_1.findFafFile)();
    if (!fafPath) {
        results.push({
            status: 'error',
            message: 'No .faf file found',
            fix: 'Run: faf init, faf quick, or faf chat to create one'
        });
    }
    else {
        results.push({
            status: 'ok',
            message: `Found .faf at: ${fafPath}`
        });
        // Check 3: .faf file validity
        console.log(colors_1.chalk.gray('Validating .faf structure...'));
        try {
            const content = await fs_1.promises.readFile(fafPath, 'utf-8');
            const fafData = (0, yaml_1.parse)(content);
            if (!fafData) {
                results.push({
                    status: 'error',
                    message: '.faf file is empty',
                    fix: 'Run: faf init --force to regenerate'
                });
            }
            else {
                // Check for required fields
                const missingFields = [];
                if (!fafData.project?.name)
                    missingFields.push('project.name');
                if (!fafData.project?.goal)
                    missingFields.push('project.goal');
                if (missingFields.length > 0) {
                    results.push({
                        status: 'warning',
                        message: `Missing important fields: ${missingFields.join(', ')}`,
                        fix: 'Run: faf enhance or faf chat to add missing info'
                    });
                }
                else {
                    results.push({
                        status: 'ok',
                        message: '.faf structure is valid'
                    });
                }
                // Check 4: Score
                console.log(colors_1.chalk.gray('Checking AI-readiness score...'));
                const compiler = new faf_compiler_1.FafCompiler();
                const scoreResult = await compiler.compile(fafPath);
                if (scoreResult.score < 30) {
                    results.push({
                        status: 'error',
                        message: `Score too low: ${scoreResult.score}%`,
                        fix: 'Run: faf enhance to improve, or faf chat to add context'
                    });
                }
                else if (scoreResult.score < 70) {
                    results.push({
                        status: 'warning',
                        message: `Score could be better: ${scoreResult.score}%`,
                        fix: 'Target 70%+ for championship AI context'
                    });
                }
                else {
                    results.push({
                        status: 'ok',
                        message: `Great score: ${scoreResult.score}%`
                    });
                }
            }
        }
        catch (error) {
            results.push({
                status: 'error',
                message: '.faf file is corrupted or invalid YAML',
                fix: 'Run: faf recover or faf init --force'
            });
        }
    }
    // Check 5: .fafignore exists
    console.log(colors_1.chalk.gray('Checking .fafignore...'));
    const fafIgnorePath = path.join(process.cwd(), '.fafignore');
    if (!await (0, file_utils_1.fileExists)(fafIgnorePath)) {
        results.push({
            status: 'warning',
            message: 'No .fafignore file',
            fix: '.fafignore helps exclude unnecessary files'
        });
    }
    else {
        results.push({
            status: 'ok',
            message: '.fafignore found'
        });
    }
    // Check 6: Project detection
    console.log(colors_1.chalk.gray('Checking project detection...'));
    const packageJsonPath = path.join(process.cwd(), 'package.json');
    const requirementsPath = path.join(process.cwd(), 'requirements.txt');
    const goModPath = path.join(process.cwd(), 'go.mod');
    if (await (0, file_utils_1.fileExists)(packageJsonPath)) {
        results.push({
            status: 'ok',
            message: 'Node.js/JavaScript project detected'
        });
    }
    else if (await (0, file_utils_1.fileExists)(requirementsPath)) {
        results.push({
            status: 'ok',
            message: 'Python project detected'
        });
    }
    else if (await (0, file_utils_1.fileExists)(goModPath)) {
        results.push({
            status: 'ok',
            message: 'Go project detected'
        });
    }
    else {
        results.push({
            status: 'warning',
            message: 'No standard project files detected',
            fix: 'FAF works best with package.json, requirements.txt, or go.mod'
        });
    }
    // Display Results
    console.log();
    console.log(championship_style_1.FAF_COLORS.fafWhite('━'.repeat(50)));
    console.log();
    console.log(championship_style_1.FAF_COLORS.fafCyan('📊 Diagnosis Results:'));
    console.log();
    let hasErrors = false;
    let hasWarnings = false;
    for (const result of results) {
        const icon = result.status === 'ok' ? '✅' :
            result.status === 'warning' ? '⚠️ ' : '❌';
        const color = result.status === 'ok' ? colors_1.chalk.green :
            result.status === 'warning' ? colors_1.chalk.yellow : colors_1.chalk.red;
        console.log(color(`${icon} ${result.message}`));
        if (result.fix) {
            console.log(colors_1.chalk.gray(`   💡 ${result.fix}`));
        }
        if (result.status === 'error')
            hasErrors = true;
        if (result.status === 'warning')
            hasWarnings = true;
    }
    // Summary
    console.log();
    console.log(championship_style_1.FAF_COLORS.fafWhite('━'.repeat(50)));
    console.log();
    if (!hasErrors && !hasWarnings) {
        console.log(championship_style_1.FAF_COLORS.fafGreen(`${championship_style_1.FAF_ICONS.trophy} Perfect health! Your FAF setup is championship-ready!`));
    }
    else if (!hasErrors) {
        console.log(championship_style_1.FAF_COLORS.fafOrange(`${championship_style_1.FAF_ICONS.precision} Good health with minor improvements suggested.`));
    }
    else {
        console.log(colors_1.chalk.red(`⚠️  Issues detected. Follow the fixes above.`));
    }
    console.log();
}
//# sourceMappingURL=doctor.js.map