/*
 * 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.handoff.list');
export default class HandoffList 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'),
        }),
        from: Flags.string({
            summary: messages.getMessage('flags.from.summary'),
            description: messages.getMessage('flags.from.description'),
        }),
        to: Flags.string({
            summary: messages.getMessage('flags.to.summary'),
            description: messages.getMessage('flags.to.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(HandoffList);
        const cwd = process.cwd();
        const file = recordPath(cwd, 'handoff');
        const owner = flags.profile ?? flags.role;
        const records = listRecords(cwd, 'handoff')
            .filter((record) => matchesTask(record, flags.task))
            .filter((record) => (owner ? record.from === profileOwner(owner) || record.to === profileOwner(owner) : true))
            .filter((record) => (flags.from ? record.from === profileOwner(flags.from) : true))
            .filter((record) => (flags.to ? record.to === profileOwner(flags.to) : true))
            .slice(-flags.limit);
        if (records.length === 0) {
            this.log(messages.getMessage('info.empty'));
        }
        else {
            const displayed = records.slice(-flags.limit);
            for (const record of displayed)
                this.log(formatRecord(record));
            if (records.length > flags.limit) {
                this.log(messages.getMessage('info.truncated', [String(records.length), file]));
            }
        }
        return { records, file, cwd };
    }
}
function matchesTask(record, task) {
    return task ? record.taskIds.includes(task) : true;
}
function formatRecord(record) {
    return [record.createdAt, record.id, `${record.from}->${record.to}`, record.taskIds.join(',') || '-', record.summary]
        .filter(Boolean)
        .join('  ');
}
//# sourceMappingURL=list.js.map