"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const chalk_1 = __importDefault(require("chalk"));
const ora_1 = __importDefault(require("ora"));
const cli_app_service_1 = require("../../lib/services/cli-app.service");
const resolve_cluster_1 = require("../../lib/resolve-cluster");
class AppScale extends core_1.Command {
    async run() {
        const { args, flags } = await this.parse(AppScale);
        const spinner = (0, ora_1.default)(`Scaling "${args.name}" to ${flags.replicas} replica(s)...`).start();
        try {
            const { id: clusterId } = await (0, resolve_cluster_1.resolveCluster)(flags.cluster);
            const service = await cli_app_service_1.CliAppService.create(clusterId);
            const app = await service.getAppByName(args.name);
            const runtime = await service.scale(app.id, flags.replicas);
            spinner.succeed(`Scaled "${args.name}" to ${flags.replicas} replica(s)`);
            const r = runtime.replicas;
            console.log('');
            console.log(`  ${chalk_1.default.bold('Desired:')}  ${r.desired ?? flags.replicas}`);
            console.log(`  ${chalk_1.default.bold('Ready:')}    ${r.ready ?? 0}`);
            console.log('');
        }
        catch (error) {
            spinner.fail('Failed to scale application');
            console.log(chalk_1.default.red(`\n  Error: ${error.message}\n`));
            this.exit(1);
        }
    }
}
AppScale.description = 'Scale an application to a desired replica count';
AppScale.examples = [
    '<%= config.bin %> <%= command.id %> my-api --replicas 3',
    '<%= config.bin %> <%= command.id %> my-api --replicas 0',
];
AppScale.args = {
    name: core_1.Args.string({
        description: 'Application name or slug',
        required: true,
    }),
};
AppScale.flags = {
    cluster: core_1.Flags.string({
        char: 'c',
        description: 'Cluster name or ID (default: auto-detect when only one cluster exists)',
    }),
    replicas: core_1.Flags.integer({
        char: 'r',
        description: 'Desired replica count (0 = stop all pods)',
        required: true,
        min: 0,
        max: 20,
    }),
};
exports.default = AppScale;
