"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 backup_client_1 = require("../../../lib/backup-client");
const context_banner_1 = require("../../../lib/context-banner");
const TARGET_KINDS = [
    'cluster',
    'namespace',
    'application',
    'observability',
];
const STRATEGIES = ['velero_rebuild', 'os_snapshot'];
class BackupRestoreCreate extends core_1.Command {
    async run() {
        const { flags } = await this.parse(BackupRestoreCreate);
        (0, context_banner_1.printContextBanner)();
        const client = backup_client_1.BackupClient.fromConfig();
        let selector;
        if (flags['target-namespace']) {
            selector = { namespace: flags['target-namespace'] };
        }
        else if (flags['target-app']) {
            selector = { applicationId: flags['target-app'] };
        }
        const spinner = (0, ora_1.default)('Creating restore job...').start();
        try {
            const r = await client.createRestore({
                artifactId: flags.artifact,
                sourceDestinationId: flags['source-destination'],
                targetClusterId: flags['target-cluster'],
                targetKind: flags['target-kind'],
                targetSelector: selector,
                strategy: flags.strategy,
            });
            spinner.succeed(`Created restore ${chalk_1.default.cyan(r.id)} (status=${r.status})`);
        }
        catch (err) {
            spinner.fail(`Create failed: ${err.message}`);
            this.exit(1);
        }
    }
}
BackupRestoreCreate.description = 'Create a restore job';
BackupRestoreCreate.flags = {
    artifact: core_1.Flags.string({
        required: true,
        description: 'Backup artifact ID',
    }),
    'source-destination': core_1.Flags.string({ required: true }),
    'target-cluster': core_1.Flags.string({
        required: true,
        description: 'Cluster to restore into',
    }),
    'target-kind': core_1.Flags.string({ required: true, options: [...TARGET_KINDS] }),
    'target-namespace': core_1.Flags.string({
        description: 'Required when --target-kind=namespace',
    }),
    'target-app': core_1.Flags.string({
        description: 'Required when --target-kind=application',
    }),
    strategy: core_1.Flags.string({ options: [...STRATEGIES] }),
};
exports.default = BackupRestoreCreate;
