/*
 * 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 { SfCommand } from '@salesforce/sf-plugins-core';
import { PHASE_TO_PROFILE, listWorkflowRuns, midPhaseGates, pausedPhaseAndNext, workflowRunPath, } from '../../../services/workflow-engine.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@jterrats/setup-agents', 'setup-agents.workflow.pending');
export default class WorkflowPending extends SfCommand {
    static summary = messages.getMessage('summary');
    static description = messages.getMessage('description');
    static examples = messages.getMessages('examples');
    async run() {
        await this.parse(WorkflowPending);
        const cwd = process.cwd();
        const file = workflowRunPath(cwd);
        const runs = listWorkflowRuns(cwd).filter((r) => r.status === 'paused' || r.status === 'running');
        const items = [];
        for (const run of runs) {
            collectTransitionGates(run, cwd, items);
            collectMidPhaseGates(run, items);
        }
        this.logPendingItems(items);
        return { items, file, cwd };
    }
    logPendingItems(items) {
        if (items.length === 0) {
            this.log(messages.getMessage('info.empty'));
            return;
        }
        this.log(messages.getMessage('info.header', [items.length]));
        for (const item of items) {
            this.log('');
            if (item.kind === 'transition') {
                this.log(messages.getMessage('info.item', [item.run.id, item.run.storyId, item.pausedPhase, item.nextPhase ?? '?']));
            }
            else {
                this.log(messages.getMessage('info.item-mid', [item.run.id, item.run.storyId, item.pausedPhase, item.reviewId]));
            }
            this.log(messages.getMessage('info.approve', [item.approveCommand]));
            this.log(messages.getMessage('info.resume', [item.resumeCommand]));
        }
    }
}
function collectTransitionGates(run, cwd, items) {
    if (run.status !== 'paused')
        return;
    const gate = pausedPhaseAndNext(run, cwd);
    if (!gate)
        return;
    const reviewer = PHASE_TO_PROFILE[gate.nextPhase];
    const approveCommand = `sf setup-agents review complete --id ${gate.reviewId} --result approved --reviewer ${reviewer}`;
    const resumeCommand = `sf setup-agents workflow run --story ${run.storyId} --resume ${run.id}`;
    items.push({
        run,
        pausedPhase: gate.pausedPhase,
        nextPhase: gate.nextPhase,
        reviewId: gate.reviewId,
        kind: 'transition',
        approveCommand,
        resumeCommand,
    });
}
function collectMidPhaseGates(run, items) {
    for (const midGate of midPhaseGates(run)) {
        const phaseRecord = run.phases.find((p) => p.phase === midGate.phase);
        const reviewer = phaseRecord ? PHASE_TO_PROFILE[midGate.phase] : 'reviewer';
        const approveCommand = `sf setup-agents review complete --id ${midGate.reviewId} --result approved --reviewer ${reviewer}`;
        const resumeCommand = `sf setup-agents workflow run --story ${run.storyId} --resume ${run.id}`;
        items.push({
            run,
            pausedPhase: midGate.phase,
            nextPhase: undefined,
            reviewId: midGate.reviewId,
            kind: 'mid-phase',
            approveCommand,
            resumeCommand,
        });
    }
}
//# sourceMappingURL=pending.js.map