/*
 * 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 { resolveWorkspaceRoot } from '../../../services/workspace-root.js';
import { workspaceFlags } from '../../../util/task-flags.js';
import { EVIDENCE_KINDS } from '../../../domain/index.js';
import { ALL_PROFILES } from '../../../profiles/index.js';
import { addEvidenceRecord, recordPath } from '../../../services/orchestration-records.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@jterrats/setup-agents', 'setup-agents.evidence.add');
const PROFILE_HELP_VALUE = ALL_PROFILES.map((profile) => profile.id).join('|');
export default class EvidenceAdd extends SfCommand {
    static summary = messages.getMessage('summary');
    static description = messages.getMessage('description');
    static examples = messages.getMessages('examples');
    static flags = {
        ...workspaceFlags,
        summary: Flags.string({
            char: 's',
            summary: messages.getMessage('flags.summary.summary'),
            description: messages.getMessage('flags.summary.description'),
            required: true,
        }),
        task: Flags.string({
            char: 't',
            summary: messages.getMessage('flags.task.summary'),
            description: messages.getMessage('flags.task.description'),
        }),
        role: Flags.string({
            char: 'r',
            summary: messages.getMessage('flags.role.summary'),
            description: messages.getMessage('flags.role.description'),
            default: 'developer',
        }),
        profile: Flags.string({
            char: 'p',
            summary: messages.getMessage('flags.profile.summary'),
            description: messages.getMessage('flags.profile.description'),
            helpValue: PROFILE_HELP_VALUE,
        }),
        type: Flags.string({
            summary: messages.getMessage('flags.type.summary'),
            description: messages.getMessage('flags.type.description'),
            options: [...EVIDENCE_KINDS],
            default: 'other',
        }),
        kind: Flags.string({
            summary: messages.getMessage('flags.kind.summary'),
            description: messages.getMessage('flags.kind.description'),
            options: [...EVIDENCE_KINDS],
        }),
        source: Flags.string({
            summary: messages.getMessage('flags.source.summary'),
            description: messages.getMessage('flags.source.description'),
        }),
        command: Flags.string({
            summary: messages.getMessage('flags.command.summary'),
            description: messages.getMessage('flags.command.description'),
        }),
        path: Flags.string({
            summary: messages.getMessage('flags.path.summary'),
            description: messages.getMessage('flags.path.description'),
        }),
        'exit-code': Flags.integer({
            summary: messages.getMessage('flags.exit-code.summary'),
            description: messages.getMessage('flags.exit-code.description'),
        }),
        details: Flags.string({
            summary: messages.getMessage('flags.details.summary'),
            description: messages.getMessage('flags.details.description'),
        }),
    };
    async run() {
        const { flags } = await this.parse(EvidenceAdd);
        const cwd = resolveWorkspaceRoot(flags);
        const evidenceKind = (flags.kind ?? flags.type);
        const sourceFields = resolveEvidenceSource(evidenceKind, flags.source);
        const input = {
            summary: flags.summary,
            profile: flags.profile,
            taskIds: toTaskIds(flags.task),
            evidenceKind,
            command: flags.command,
            path: flags.path,
            exitCode: flags['exit-code'],
            details: flags.details,
            ...sourceFields,
        };
        const record = addEvidenceRecord(cwd, { ...input, profile: input.profile ?? flags.role });
        const file = recordPath(cwd, 'evidence');
        this.log(messages.getMessage('info.created', [record.id, file]));
        return { record, file, cwd };
    }
}
function toTaskIds(task) {
    return task ? [task] : [];
}
function resolveEvidenceSource(evidenceKind, source) {
    if (!source)
        return {};
    return evidenceKind === 'command' ? { command: source } : { path: source };
}
//# sourceMappingURL=add.js.map