/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { Messages } from '@salesforce/core';
import { Flags, SfCommand } from '@salesforce/sf-plugins-core';
import { runReleaseChecks } from '../../../services/release-check.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@jterrats/setup-agents', 'setup-agents.workflow.release-check');
const ALL_CHECK_NAMES = ['npm-audit', 'knip', 'coverage', 'secret-scan'];
const STATUS_ICON = {
    pass: '✓',
    warn: '⚠',
    fail: '✗',
};
export default class WorkflowReleaseCheck extends SfCommand {
    static summary = messages.getMessage('summary');
    static description = messages.getMessage('description');
    static examples = messages.getMessages('examples');
    static flags = {
        task: Flags.string({
            summary: messages.getMessage('flags.task.summary'),
            description: messages.getMessage('flags.task.description'),
            required: true,
        }),
        'dry-run': Flags.boolean({
            summary: messages.getMessage('flags.dry-run.summary'),
            description: messages.getMessage('flags.dry-run.description'),
            default: false,
        }),
        checks: Flags.string({
            summary: messages.getMessage('flags.checks.summary'),
            description: messages.getMessage('flags.checks.description'),
        }),
    };
    async run() {
        const { flags } = await this.parse(WorkflowReleaseCheck);
        const cwd = process.cwd();
        const taskId = flags.task;
        if (flags['dry-run']) {
            this.log(messages.getMessage('info.dry-run'));
            const dryReport = {
                taskId,
                checks: ALL_CHECK_NAMES.map((name) => ({
                    name,
                    status: 'pass',
                    blocking: false,
                    detail: 'skipped (dry-run)',
                })),
                overallPass: true,
                runAt: new Date().toISOString(),
            };
            this.printTable(dryReport.checks);
            return { report: dryReport, cwd };
        }
        this.log(messages.getMessage('info.header', [taskId]));
        const checksToRun = resolveChecks(flags.checks);
        const report = await runReleaseChecks(cwd, taskId, checksToRun);
        this.printTable(report.checks);
        if (report.overallPass) {
            this.log(messages.getMessage('info.pass'));
        }
        else {
            this.error(messages.getMessage('errors.blocking-failure'), { exit: 1 });
        }
        return { report, cwd };
    }
    printTable(checks) {
        const col1 = Math.max(5, ...checks.map((c) => c.name.length));
        const col4 = Math.max(6, ...checks.map((c) => c.detail.length));
        const line = (name, status, blocking, detail) => {
            const icon = STATUS_ICON[status] ?? status;
            return `${name.padEnd(col1)}  ${(icon + ' ' + status).padEnd(8)}  ${(blocking ? 'yes' : 'no').padEnd(8)}  ${detail.padEnd(col4)}`;
        };
        this.log('');
        this.log(line('Check', 'Status', false, 'Detail').replace(/ {2}no {2}/, '  Block '));
        this.log('-'.repeat(col1 + col4 + 28));
        for (const c of checks) {
            this.log(line(c.name, c.status, c.blocking, c.detail));
        }
        this.log('');
    }
}
function resolveChecks(raw) {
    if (!raw)
        return ALL_CHECK_NAMES;
    return raw
        .split(',')
        .map((s) => s.trim())
        .filter((s) => ALL_CHECK_NAMES.includes(s));
}
//# sourceMappingURL=release-check.js.map