"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 AppSnapshotRestore extends core_1.Command {
    async run() {
        const { args, flags } = await this.parse(AppSnapshotRestore);
        const spinner = (0, ora_1.default)(`Restoring snapshot ${args.snapshotId}...`).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 result = await service.restoreAppSnapshot(app.id, args.snapshotId);
            spinner.succeed(`Restored to new PVC: ${result.newPvcName}`);
            console.log('');
            console.log(`  ${chalk_1.default.bold('App:')}        ${app.name}`);
            console.log(`  ${chalk_1.default.bold('From snap:')}  ${args.snapshotId}`);
            console.log(`  ${chalk_1.default.bold('New PVC:')}    ${result.newPvcName}`);
            if (flags.swap) {
                const volumeName = flags.volume ?? 'data';
                const swapSpinner = (0, ora_1.default)(`Swapping volume "${volumeName}" to ${result.newPvcName}...`).start();
                await service.swapAppVolume(app.id, volumeName, result.newPvcName);
                swapSpinner.succeed('Volume swapped, rollout 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 no longer needed.`));
            }
            else {
                console.log('');
                console.log(chalk_1.default.dim('  Next steps:'));
                console.log(chalk_1.default.dim(`  flui app snapshot swap ${app.name} ${result.newPvcName}`));
            }
        }
        catch (error) {
            spinner.fail('Restore failed');
            const msg = error.response?.data?.message ?? error.message ?? String(error);
            console.log(chalk_1.default.red(`\n  Error: ${msg}\n`));
            this.exit(1);
        }
    }
}
AppSnapshotRestore.description = 'Restore a snapshot into a new side-by-side PVC. The application is not touched — the new PVC is created in the same namespace and can be promoted later with `flui app snapshot swap`.';
AppSnapshotRestore.examples = [
    '<%= config.bin %> <%= command.id %> my-app my-app-snap-20260510-abcdef',
    '<%= config.bin %> <%= command.id %> my-app my-app-snap-... --swap',
];
AppSnapshotRestore.args = {
    name: core_1.Args.string({
        description: 'Application name or slug',
        required: true,
    }),
    snapshotId: core_1.Args.string({
        description: 'Snapshot id (from `flui app snapshot list`)',
        required: true,
    }),
};
AppSnapshotRestore.flags = {
    cluster: core_1.Flags.string({
        char: 'c',
        description: 'Cluster name or ID (default: auto-detect)',
    }),
    swap: core_1.Flags.boolean({
        description: 'Immediately swap the restored PVC into the live application (rolling restart). Old PVC is preserved as a backup.',
    }),
    volume: core_1.Flags.string({
        char: 'v',
        description: 'Application volume name to swap when --swap is set. Required if the app has multiple volumes.',
    }),
};
exports.default = AppSnapshotRestore;
