import path from 'node:path';
import { ExitCode, sair } from '../helpers/index.js';
import { getMessages } from '../../core/messages/index.js';
import * as licensas from '../../licenses/index.js';
import { Command } from 'commander';
const { log, CliComandoLicencasMensagens } = getMessages();
const msgs = CliComandoLicencasMensagens;
export function comandoLicencas() {
    const cmd = new Command('licencas').description(msgs.descricao);
    cmd.allowUnknownOption(true);
    cmd.allowExcessArguments(true);
    cmd.command('scan').description(msgs.scanDescricao).option('--root <path>', 'diretório raiz (padrão: cwd)').action(async (opts) => {
        try {
            const root = opts.root ? path.resolve(opts.root) : process.cwd();
            const result = await licensas.scanCommand({
                root
            });
            console.log(JSON.stringify(result, null, 2));
            process.exitCode = result.problematic && result.problematic.length > 0 ? 2 : 0;
        }
        catch (err) {
            log.erro(msgs.falhaScan(err instanceof Error ? err.message : String(err)));
            sair(ExitCode.Failure);
        }
    });
    const notices = cmd.command('notices').description(msgs.noticesDescricao);
    notices.command('generate').description(msgs.generateDescricao).option('--pt-br', 'usar cabeçalho em português').option('--output <file>', 'arquivo de saída').option('--root <path>', 'pasta do projeto').action(async (opts) => {
        try {
            const root = opts.root ? path.resolve(opts.root) : process.cwd();
            const res = await licensas.generateNotices({
                root,
                ptBr: Boolean(opts.ptBr),
                output: opts.output
            });
            console.log(msgs.generatedNotices, res);
            sair(ExitCode.Ok);
        }
        catch (err) {
            log.erro(msgs.falhaGenerate(err instanceof Error ? err.message : String(err)));
            sair(ExitCode.Failure);
        }
    });
    const disclaimer = cmd.command('disclaimer').description(msgs.disclaimerDescricao);
    disclaimer.command('add').description(msgs.addDisclaimerDescricao).option('--disclaimer-path <path>', 'caminho do arquivo de disclaimer').option('--root <path>', 'pasta do projeto').option('--dry-run', msgs.dryRunDescricao).action(async (opts) => {
        try {
            const root = opts.root ? path.resolve(opts.root) : process.cwd();
            const res = await licensas.addDisclaimer({
                root,
                disclaimerPath: opts.disclaimerPath,
                dryRun: Boolean(opts.dryRun)
            });
            console.log(msgs.disclaimerInserted, res.updatedArquivos.length);
            sair(ExitCode.Ok);
        }
        catch (err) {
            log.erro(msgs.falhaAddDisclaimer(err instanceof Error ? err.message : String(err)));
            sair(ExitCode.Failure);
        }
    });
    disclaimer.command('verify').description(msgs.verifyDescricao).option('--disclaimer-path <path>').option('--root <path>').action(async (opts) => {
        try {
            const root = opts.root ? path.resolve(opts.root) : process.cwd();
            const res = await licensas.verifyDisclaimer({
                root,
                disclaimerPath: opts.disclaimerPath
            });
            if (res.missing.length) {
                console.error(msgs.missingDisclaimer);
                for (const arquivo of res.missing)
                    console.error('-', arquivo);
                sair(ExitCode.Failure);
                return;
            }
            console.log(msgs.allIncludeDisclaimer);
            sair(ExitCode.Ok);
        }
        catch (err) {
            log.erro(msgs.falhaVerifyDisclaimer(err instanceof Error ? err.message : String(err)));
            sair(ExitCode.Failure);
        }
    });
    return cmd;
}
//# sourceMappingURL=command-licenses.js.map