/*
 * 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 { PHASE_SEQUENCE, readWorkflowRun, rollbackWorkflowRun, workflowRunPath, } from '../../../services/workflow-engine.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@jterrats/setup-agents', 'setup-agents.workflow.rollback');
export default class WorkflowRollback extends SfCommand {
    static summary = messages.getMessage('summary');
    static description = messages.getMessage('description');
    static examples = messages.getMessages('examples');
    static flags = {
        run: Flags.string({
            summary: messages.getMessage('flags.run.summary'),
            description: messages.getMessage('flags.run.description'),
            required: true,
        }),
        phase: Flags.string({
            summary: messages.getMessage('flags.phase.summary'),
            description: messages.getMessage('flags.phase.description'),
            options: [...PHASE_SEQUENCE],
            required: true,
        }),
        reason: Flags.string({
            summary: messages.getMessage('flags.reason.summary'),
            description: messages.getMessage('flags.reason.description'),
            default: '',
        }),
    };
    async run() {
        const { flags } = await this.parse(WorkflowRollback);
        const cwd = process.cwd();
        const file = workflowRunPath(cwd);
        const run = readWorkflowRun(cwd, flags.run);
        if (!run)
            throw new Error(messages.getMessage('errors.run-not-found', [flags.run]));
        if (run.status === 'done')
            throw new Error(messages.getMessage('errors.run-done', [flags.run]));
        const targetPhase = flags.phase;
        const phaseRecord = run.phases.find((p) => p.phase === targetPhase);
        if (!phaseRecord)
            throw new Error(messages.getMessage('errors.phase-not-found', [targetPhase, flags.run]));
        const rollbackResult = rollbackWorkflowRun(cwd, run, targetPhase, flags.reason);
        if (!rollbackResult.ok)
            throw new Error(rollbackResult.error.message);
        const updated = rollbackResult.value;
        this.log(messages.getMessage('info.rolledBack', [flags.run, targetPhase]));
        this.log(messages.getMessage('info.resume', [updated.storyId, updated.id]));
        return { run: updated, targetPhase, reason: flags.reason, file, cwd };
    }
}
//# sourceMappingURL=rollback.js.map