"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");
const prompts_1 = require("../../../lib/prompts");
class AppSnapshotSwap extends core_1.Command {
    async run() {
        const { args, flags } = await this.parse(AppSnapshotSwap);
        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 volumeName = flags.volume ?? 'data';
            if (!flags.force) {
                const ok = await (0, prompts_1.confirmPrompt)(`Swap volume "${volumeName}" of "${args.name}" to PVC "${args.newPvcName}"? This triggers a rolling restart.`);
                if (!ok) {
                    console.log(chalk_1.default.dim('  Aborted.'));
                    return;
                }
            }
            const spinner = (0, ora_1.default)(`Swapping ${volumeName} → ${args.newPvcName}...`).start();
            await service.swapAppVolume(app.id, volumeName, args.newPvcName);
            spinner.succeed('Swap applied, rolling restart triggered');
            console.log(chalk_1.default.dim(`\n  Old PVC kept as backup. Use \`flui env kubectl get pvc -n <ns>\` to inspect, delete when ready.`));
        }
        catch (error) {
            const msg = error.response?.data?.message ?? error.message ?? String(error);
            console.log(chalk_1.default.red(`\n  Error: ${msg}\n`));
            this.exit(1);
        }
    }
}
AppSnapshotSwap.description = 'Swap the application volume to a different PVC (typically one created by `flui app snapshot restore`). Triggers a rolling restart. The previous PVC is preserved as a backup.';
AppSnapshotSwap.examples = [
    '<%= config.bin %> <%= command.id %> my-app my-app-data-restored-20260511',
    '<%= config.bin %> <%= command.id %> my-app new-pvc --volume data --force',
];
AppSnapshotSwap.args = {
    name: core_1.Args.string({
        description: 'Application name or slug',
        required: true,
    }),
    newPvcName: core_1.Args.string({
        description: 'PVC name to swap into the application',
        required: true,
    }),
};
AppSnapshotSwap.flags = {
    cluster: core_1.Flags.string({
        char: 'c',
        description: 'Cluster name or ID (default: auto-detect)',
    }),
    volume: core_1.Flags.string({
        char: 'v',
        description: 'Application volume name (default: the single volume)',
    }),
    force: core_1.Flags.boolean({
        char: 'f',
        description: 'Skip confirmation',
    }),
};
exports.default = AppSnapshotSwap;
