import { buildCommandContext, } from '../../app/command-context.js';
import { readGlobalOptions } from '../shared/shared.utils.js';
import { validateChangedSkillVersionBumps as defaultValidateChangedSkillVersionBumps, } from '../../validation/index.js';
import { Command } from 'commander';
const DEFAULT_DEPENDENCIES = {
    buildCommandContext,
    validateChangedSkillVersionBumps: defaultValidateChangedSkillVersionBumps,
};
function reportFindings(context, baseRef, result) {
    if (context.json) {
        context.logger.json({
            status: 'failed',
            baseRef,
            validatedSkillCount: result.validatedSkillCount,
            findings: result.findings,
        });
        return;
    }
    context.logger.error('Canonical skill version validation failed:\n');
    for (const finding of result.findings) {
        context.logger.error(`- ${finding.file}: ${finding.message}`);
    }
    context.logger.error(`\nFix the issues above, then re-run: oat internal validate-skill-version-bumps --base-ref ${baseRef}`);
}
async function runValidateSkillVersionBumps(context, options, dependencies) {
    try {
        const result = await dependencies.validateChangedSkillVersionBumps(context.cwd, options);
        if (result.findings.length > 0) {
            reportFindings(context, options.baseRef, result);
            process.exitCode = 1;
            return;
        }
        if (context.json) {
            context.logger.json({
                status: 'ok',
                baseRef: options.baseRef,
                validatedSkillCount: result.validatedSkillCount,
                findings: result.findings,
            });
        }
        else {
            if (result.validatedSkillCount === 0) {
                context.logger.info(`OK: 0 canonical skills changed relative to ${options.baseRef} - nothing to validate`);
            }
            else {
                context.logger.info(`OK: validated ${result.validatedSkillCount} changed canonical skill version bump checks against ${options.baseRef}`);
            }
        }
        process.exitCode = 0;
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        if (context.json) {
            context.logger.json({ status: 'error', message });
        }
        else {
            context.logger.error(message);
        }
        process.exitCode = 2;
    }
}
export function createValidateSkillVersionBumpsCommand(overrides = {}) {
    const dependencies = {
        ...DEFAULT_DEPENDENCIES,
        ...overrides,
    };
    return new Command('validate-skill-version-bumps')
        .description('Validate that changed canonical skills bump version relative to a git base ref')
        .requiredOption('--base-ref <ref>', 'Git ref used as the comparison base for changed canonical skills')
        .action(async (options, command) => {
        const context = dependencies.buildCommandContext(readGlobalOptions(command));
        await runValidateSkillVersionBumps(context, options, dependencies);
    });
}
