/*
 * 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 { TASK_ACTIONABLE_STATUSES } from '../../../domain/index.js';
import { ALL_PROFILES } from '../../../profiles/index.js';
import { getTask, patchTask, taskStatePath, transitionTask } from '../../../services/task-state.js';
import { resolveTaskId, splitCsv, taskIdentifierFlags } from '../../../util/task-flags.js';
import { resolveWorkspaceRoot } from '../../../services/workspace-root.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@jterrats/setup-agents', 'setup-agents.task.update');
const PROFILE_HELP_VALUE = ALL_PROFILES.map((profile) => profile.id).join('|');
export default class TaskUpdate extends SfCommand {
    static summary = messages.getMessage('summary');
    static description = messages.getMessage('description');
    static examples = messages.getMessages('examples');
    static flags = {
        ...taskIdentifierFlags,
        status: Flags.string({
            summary: messages.getMessage('flags.status.summary'),
            description: messages.getMessage('flags.status.description'),
            options: [...TASK_ACTIONABLE_STATUSES],
        }),
        reason: Flags.string({
            summary: messages.getMessage('flags.reason.summary'),
            description: messages.getMessage('flags.reason.description'),
        }),
        summary: Flags.string({
            char: 's',
            summary: messages.getMessage('flags.summary.summary'),
            description: messages.getMessage('flags.summary.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'),
            helpValue: PROFILE_HELP_VALUE,
        }),
        evidence: Flags.string({
            summary: messages.getMessage('flags.evidence.summary'),
            description: messages.getMessage('flags.evidence.description'),
        }),
    };
    async run() {
        const { flags } = await this.parse(TaskUpdate);
        const cwd = resolveWorkspaceRoot(flags);
        const file = taskStatePath(cwd);
        const taskId = resolveTaskId(flags);
        if (!getTask(cwd, taskId))
            throw new Error(messages.getMessage('errors.notFound', [taskId]));
        let record;
        if (flags.status) {
            record = transitionTask(cwd, {
                id: taskId,
                status: flags.status,
                profile: flags.profile ?? flags.role,
                reason: flags.reason,
                evidenceIds: splitCsv(flags.evidence),
            });
        }
        else {
            record = patchTask(cwd, {
                id: taskId,
                summary: flags.summary,
                profile: flags.profile ?? flags.role,
                evidenceIds: splitCsv(flags.evidence),
            });
        }
        this.log(messages.getMessage('info.updated', [record.id, record.status]));
        return { record, file, cwd };
    }
}
//# sourceMappingURL=update.js.map