"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 AppRestart extends core_1.Command {
    async run() {
        const { args, flags } = await this.parse(AppRestart);
        const spinner = (0, ora_1.default)(`Restarting "${args.name}"...`).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);
            await service.restart(app.id);
            spinner.succeed(`Rolling restart triggered for "${args.name}"`);
            console.log(chalk_1.default.dim('\n  New pods will replace existing ones one by one.\n'));
        }
        catch (error) {
            spinner.fail('Failed to restart application');
            console.log(chalk_1.default.red(`\n  Error: ${error.message}\n`));
            this.exit(1);
        }
    }
}
AppRestart.description = 'Trigger a rolling restart of an application';
AppRestart.examples = ['<%= config.bin %> <%= command.id %> my-api'];
AppRestart.args = {
    name: core_1.Args.string({
        description: 'Application name or slug',
        required: true,
    }),
};
AppRestart.flags = {
    cluster: core_1.Flags.string({
        char: 'c',
        description: 'Cluster name or ID (default: auto-detect when only one cluster exists)',
    }),
};
exports.default = AppRestart;
