/*
 * 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 { estimatesPath, historicalEstimateStats, recordEstimate } from '../../../services/workflow-benchmark.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@jterrats/setup-agents', 'setup-agents.workflow.estimate');
const SIZING_LABELS = ['xs', 's', 'm', 'l', 'xl'];
const CONFIDENCE_LEVELS = ['low', 'medium', 'high'];
const parseDays = (raw, flag) => {
    const n = Number(raw);
    if (isNaN(n) || n <= 0)
        throw new Error(`--${flag} must be a positive number (e.g. 2.5)`);
    return n;
};
export default class WorkflowEstimate 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'),
            required: true,
        }),
        sizing: Flags.string({
            summary: messages.getMessage('flags.sizing.summary'),
            description: messages.getMessage('flags.sizing.description'),
            options: [...SIZING_LABELS],
            required: true,
        }),
        'solo-days': Flags.string({
            summary: messages.getMessage('flags.solo-days.summary'),
            description: messages.getMessage('flags.solo-days.description'),
            required: true,
        }),
        'ai-unguided-days': Flags.string({
            summary: messages.getMessage('flags.ai-unguided-days.summary'),
            description: messages.getMessage('flags.ai-unguided-days.description'),
            required: true,
        }),
        'ai-guided-days': Flags.string({
            summary: messages.getMessage('flags.ai-guided-days.summary'),
            description: messages.getMessage('flags.ai-guided-days.description'),
        }),
        confidence: Flags.string({
            summary: messages.getMessage('flags.confidence.summary'),
            description: messages.getMessage('flags.confidence.description'),
            options: [...CONFIDENCE_LEVELS],
            default: 'medium',
        }),
        'declared-by': Flags.string({
            summary: messages.getMessage('flags.declared-by.summary'),
            description: messages.getMessage('flags.declared-by.description'),
            default: 'pm',
        }),
        'ignore-history': Flags.boolean({
            summary: messages.getMessage('flags.ignore-history.summary'),
            default: false,
        }),
    };
    async run() {
        const { flags } = await this.parse(WorkflowEstimate);
        const cwd = process.cwd();
        const file = estimatesPath(cwd);
        const soloEstimateDays = parseDays(flags['solo-days'], 'solo-days');
        const aiUnguidedEstimateDays = parseDays(flags['ai-unguided-days'], 'ai-unguided-days');
        const aiGuidedDays = flags['ai-guided-days'] ? parseDays(flags['ai-guided-days'], 'ai-guided-days') : undefined;
        const record = recordEstimate(cwd, {
            storyId: flags.story,
            sizingLabel: flags.sizing,
            soloEstimateDays,
            aiUnguidedEstimateDays,
            aiGuidedDays,
            confidence: flags.confidence,
            declaredBy: flags['declared-by'],
        });
        this.log(messages.getMessage('info.created', [
            record.id,
            record.storyId,
            record.sizingLabel,
            record.soloEstimateDays,
            record.aiUnguidedEstimateDays,
            record.confidence,
        ]));
        const historicalStats = flags['ignore-history'] ? undefined : historicalEstimateStats(cwd);
        if (historicalStats && historicalStats.count >= 2) {
            this.log('');
            this.log(`Historical baseline (${historicalStats.count} completed runs):`);
            if (historicalStats.medianSoloDays !== null) {
                this.log(`  Median solo days:       ${historicalStats.medianSoloDays}`);
            }
            if (historicalStats.medianAiGuidedDays !== null) {
                this.log(`  Median AI-guided days:  ${historicalStats.medianAiGuidedDays}`);
            }
            if (historicalStats.calibrationNote) {
                this.log(`  ⚠ ${historicalStats.calibrationNote}`);
            }
        }
        return { record, file, cwd, historicalStats };
    }
}
//# sourceMappingURL=estimate.js.map