/*
 * 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 { addMidPhaseGate, readWorkflowRun, workflowRunPath } from '../../../services/workflow-engine.js';
import { notifyGatePause } from '../../../services/notifications.js';
import { requestReview } from '../../../services/orchestration-records.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@jterrats/setup-agents', 'setup-agents.workflow.gate');
export default class WorkflowGate 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'),
            required: true,
        }),
        from: Flags.string({
            summary: messages.getMessage('flags.from.summary'),
            description: messages.getMessage('flags.from.description'),
            required: true,
        }),
        to: Flags.string({
            summary: messages.getMessage('flags.to.summary'),
            description: messages.getMessage('flags.to.description'),
            required: true,
        }),
        summary: Flags.string({
            char: 's',
            summary: messages.getMessage('flags.summary.summary'),
            description: messages.getMessage('flags.summary.description'),
            required: true,
        }),
        gate: Flags.string({
            summary: messages.getMessage('flags.gate.summary'),
            description: messages.getMessage('flags.gate.description'),
        }),
    };
    async run() {
        const { flags } = await this.parse(WorkflowGate);
        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]));
        const phase = flags.phase;
        const phaseRecord = run.phases.find((p) => p.phase === phase);
        if (!phaseRecord || phaseRecord.status !== 'running') {
            throw new Error(messages.getMessage('errors.phase-not-active', [phase, flags.run]));
        }
        const gateLabel = flags.gate ?? `${flags.from}→${flags.to}`;
        const review = requestReview(cwd, {
            summary: flags.summary,
            requester: flags.from,
            reviewer: flags.to,
            gates: [gateLabel],
            taskIds: [run.storyId],
        });
        const updated = addMidPhaseGate(cwd, run, phase, review.id);
        this.log(messages.getMessage('info.created', [review.id]));
        this.log(messages.getMessage('info.resolve', [review.id, flags.to]));
        this.log(messages.getMessage('info.resume', [updated.storyId, updated.id]));
        await notifyGatePause(cwd, {
            runId: updated.id,
            storyId: updated.storyId,
            phase,
            kind: 'mid-phase',
            fromRole: flags.from,
            toRole: flags.to,
            reviewId: review.id,
            resumeCommand: `sf setup-agents workflow run --story ${updated.storyId} --resume ${updated.id}`,
        });
        return { reviewId: review.id, run: updated, file, cwd };
    }
}
//# sourceMappingURL=gate.js.map