/*
 * 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 { computeBenchmark, estimatesPath, summarizeBenchmark } from '../../../services/workflow-benchmark.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@jterrats/setup-agents', 'setup-agents.workflow.benchmark');
const PCT_LABEL = (pct) => {
    if (pct === null)
        return '—';
    const sign = pct <= 0 ? '' : '+';
    return `${sign}${pct}%`;
};
const DAYS_LABEL = (days) => (days === null ? 'pending' : `${days}d`);
export default class WorkflowBenchmark extends SfCommand {
    static summary = messages.getMessage('summary');
    static description = messages.getMessage('description');
    static examples = messages.getMessages('examples');
    static flags = {
        story: Flags.string({
            char: 's',
            summary: messages.getMessage('flags.story.summary'),
            description: messages.getMessage('flags.story.description'),
        }),
        summary: Flags.boolean({
            summary: messages.getMessage('flags.summary.summary'),
            description: messages.getMessage('flags.summary.description'),
            default: false,
        }),
    };
    async run() {
        const { flags } = await this.parse(WorkflowBenchmark);
        const cwd = process.cwd();
        const file = estimatesPath(cwd);
        if (flags.summary) {
            const summary = summarizeBenchmark(cwd);
            if (summary.stories.length === 0) {
                this.log(messages.getMessage('info.empty'));
                return { summary, file, cwd };
            }
            this.log(messages.getMessage('info.summary-header', [summary.stories.length, summary.totalWithActuals]));
            this.log('');
            const col = (s, w) => s.padEnd(w).slice(0, w);
            const header = `${col('Story', 16)} ${col('Size', 4)} ${col('Solo', 6)} ${col('AI', 6)} ${col('Actual', 8)} ${col('vsSolo', 8)} ${col('vsAI', 8)} ${col('QA', 4)} ${col('Ev', 4)} ${col('St', 8)}`;
            this.log(header);
            this.log('─'.repeat(header.length));
            for (const s of summary.stories) {
                const soloLabel = `${s.soloEstimateDays}d`;
                const aiLabel = `${s.aiUnguidedEstimateDays}d`;
                this.log(`${col(s.storyId, 16)} ${col(s.sizingLabel, 4)} ${col(soloLabel, 6)} ${col(aiLabel, 6)} ${col(DAYS_LABEL(s.actualDays), 8)} ${col(PCT_LABEL(s.vsSoloPct), 8)} ${col(PCT_LABEL(s.vsAiUnguidedPct), 8)} ${col(String(s.qaIterations), 4)} ${col(String(s.evidenceCount), 4)} ${col(s.status, 8)}`);
            }
            this.log('');
            this.log(messages.getMessage('info.summary-averages', [
                PCT_LABEL(summary.avgVsSoloPct),
                PCT_LABEL(summary.avgVsAiUnguidedPct),
            ]));
            return { summary, file, cwd };
        }
        if (!flags.story) {
            this.error('Provide --story <id> for a single report or --summary for all stories.');
        }
        let result;
        try {
            result = computeBenchmark(cwd, flags.story);
        }
        catch {
            this.error(messages.getMessage('errors.no-estimate', [flags.story]));
        }
        this.log(messages.getMessage('info.story-header', [result.storyId]));
        this.log(messages.getMessage('info.sizing', [result.sizingLabel]));
        this.log(messages.getMessage('info.solo', [result.soloEstimateDays]));
        this.log(messages.getMessage('info.ai-unguided', [result.aiUnguidedEstimateDays]));
        this.log(messages.getMessage('info.actual', [DAYS_LABEL(result.actualDays)]));
        this.log(messages.getMessage('info.actual-source', [result.actualSource ?? '—']));
        this.log(messages.getMessage('info.vs-solo', [PCT_LABEL(result.vsSoloPct)]));
        this.log(messages.getMessage('info.vs-ai', [PCT_LABEL(result.vsAiUnguidedPct)]));
        this.log(messages.getMessage('info.qa-iterations', [result.qaIterations]));
        this.log(messages.getMessage('info.evidence', [result.evidenceCount]));
        this.log(messages.getMessage('info.status', [result.status]));
        return { result, file, cwd };
    }
}
//# sourceMappingURL=benchmark.js.map