import { updateApplicationProperty } from '../srv/update-application-property.js';
import { getApplicationOrVersionNamesCompletion } from './common/completions.js';

export const getMetadata = () => ({
    name: 'activate',
    desc: 'Activate deployed application version',
    usage: 'afctl activate APPLICATION VERSION',
    options: {
        application: {
            keys: [],
            type: 'string',
            mandatory: true,
            multiple: false,
            helpLiteral: 'APPLICATION',
            helpText: 'Name of the deployed application',
            index: 0
        },
        version: {
            keys: [],
            type: 'string',
            mandatory: true,
            multiple: false,
            helpLiteral: 'VERSION',
            helpText: 'Name of the application version',
            index: 1
        }
    }
});

export const getCompletion = getApplicationOrVersionNamesCompletion({versionCompletion: true});

export const run = async ({ args, config, console, fetch }) => {
    const profile = config?.profiles?.[config?.active_profile];
    console.debug(`Activating version '${args.version}' of application '${args.application}'`);
    await updateApplicationProperty({
        console, profile, fetch,
        application: args.application,
        property: {
            name: 'active_version_name',
            value: args.version
        }
    });
};
