"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 format_bytes_1 = require("../../../lib/format-bytes");
class AppSnapshotCreate extends core_1.Command {
    async run() {
        const { args, flags } = await this.parse(AppSnapshotCreate);
        const spinner = (0, ora_1.default)(`Creating snapshot for "${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);
            const snap = await service.createAppSnapshot(app.id, {
                volumeName: flags.volume,
                description: flags.description,
            });
            spinner.succeed(`Snapshot created: ${snap.exportId}`);
            console.log('');
            console.log(`  ${chalk_1.default.bold('Provider:')}  ${snap.provider}`);
            console.log(`  ${chalk_1.default.bold('Sink:')}      ${snap.sink}`);
            console.log(`  ${chalk_1.default.bold('Namespace:')} ${snap.namespace}`);
            if (snap.sourcePvcName) {
                console.log(`  ${chalk_1.default.bold('Source:')}    ${snap.sourcePvcName}`);
            }
            if (snap.sizeGb !== undefined) {
                const actual = snap.actualBytes === undefined
                    ? 'unknown'
                    : (0, format_bytes_1.formatBytes)(snap.actualBytes);
                console.log(`  ${chalk_1.default.bold('Source request:')}  ${snap.sizeGb} GiB`);
                console.log(`  ${chalk_1.default.bold('Disk usage:')}      ${actual}`);
            }
            console.log(`  ${chalk_1.default.bold('Ready:')}     ${snap.ready ? 'yes' : 'pending'}`);
            console.log(`  ${chalk_1.default.bold('Created:')}   ${snap.createdAt}`);
            const caps = snap.providerCapabilities;
            if (!caps.pvcCloneSupportsCheapRetention) {
                console.log('');
                console.log(chalk_1.default.yellow('  ! pvc-clone snapshots are billed as a full Volume each — delete when no longer needed:'));
                console.log(chalk_1.default.yellow(`     flui app snapshot delete ${args.name} ${snap.exportId}`));
            }
            console.log('');
        }
        catch (error) {
            spinner.fail('Snapshot creation failed');
            const msg = error.response?.data?.message ?? error.message ?? String(error);
            console.log(chalk_1.default.red(`\n  Error: ${msg}\n`));
            this.exit(1);
        }
    }
}
AppSnapshotCreate.description = 'Create a snapshot of an application volume. Today on every provider this ' +
    'is a full PVC clone built via the copy-pod export primitive (sink=pvc-clone) ' +
    'because workload PVCs use local-path. Cost: a full Volume per snapshot.';
AppSnapshotCreate.examples = [
    '<%= config.bin %> <%= command.id %> my-app',
    '<%= config.bin %> <%= command.id %> my-app --description before-upgrade',
    '<%= config.bin %> <%= command.id %> my-app --volume data',
];
AppSnapshotCreate.args = {
    name: core_1.Args.string({
        description: 'Application name or slug',
        required: true,
    }),
};
AppSnapshotCreate.flags = {
    cluster: core_1.Flags.string({
        char: 'c',
        description: 'Cluster name or ID (default: auto-detect)',
    }),
    volume: core_1.Flags.string({
        char: 'v',
        description: 'Volume (PVC) name when the app has multiple volumes. Required if more than one PVC exists.',
    }),
    description: core_1.Flags.string({
        char: 'd',
        description: 'Optional human-friendly tag appended to the snapshot id',
    }),
};
exports.default = AppSnapshotCreate;
