/*
 * 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 { listRecords, profileOwner, recordPath } from '../../../services/orchestration-records.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@jterrats/setup-agents', 'setup-agents.decision.list');
export default class DecisionList extends SfCommand {
    static summary = messages.getMessage('summary');
    static description = messages.getMessage('description');
    static examples = messages.getMessages('examples');
    static flags = {
        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'),
        }),
        profile: Flags.string({
            char: 'p',
            summary: messages.getMessage('flags.profile.summary'),
            description: messages.getMessage('flags.profile.description'),
        }),
        status: Flags.string({
            summary: messages.getMessage('flags.status.summary'),
            description: messages.getMessage('flags.status.description'),
        }),
        limit: Flags.integer({
            char: 'l',
            summary: messages.getMessage('flags.limit.summary'),
            description: messages.getMessage('flags.limit.description'),
            default: 20,
            min: 1,
        }),
    };
    async run() {
        const { flags } = await this.parse(DecisionList);
        const cwd = process.cwd();
        const file = recordPath(cwd, 'decision');
        const owner = flags.profile ?? flags.role;
        const records = listRecords(cwd, 'decision')
            .filter((record) => matchesTask(record, flags.task))
            .filter((record) => (owner ? record.owner === profileOwner(owner) : true))
            .filter((record) => (flags.status ? record.outcome === flags.status : true))
            .slice(-flags.limit);
        if (records.length === 0) {
            this.log(messages.getMessage('info.empty'));
        }
        else {
            for (const record of records)
                this.log(formatRecord(record));
        }
        return { records, file, cwd };
    }
}
function matchesTask(record, task) {
    return task ? record.taskIds.includes(task) : true;
}
function formatRecord(record) {
    return [
        record.createdAt,
        record.id,
        record.owner,
        record.taskIds.join(',') || '-',
        record.outcome ?? '',
        record.summary,
    ]
        .filter(Boolean)
        .join('  ');
}
//# sourceMappingURL=list.js.map