"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 AppSnapshotList extends core_1.Command {
    async run() {
        const { flags } = await this.parse(AppSnapshotList);
        const spinner = (0, ora_1.default)('Fetching snapshots...').start();
        try {
            const { id: clusterId } = await (0, resolve_cluster_1.resolveCluster)(flags.cluster);
            const service = await cli_app_service_1.CliAppService.create(clusterId);
            let items;
            if (flags.app) {
                const app = await service.getAppByName(flags.app);
                items = await service.listAppSnapshots(app.id);
            }
            else {
                items = await service.listClusterSnapshots();
            }
            spinner.stop();
            if (flags.output === 'json') {
                console.log(JSON.stringify(items, null, 2));
                return;
            }
            if (items.length === 0) {
                console.log(chalk_1.default.dim('  No snapshots found.'));
                return;
            }
            console.log('');
            console.log(`  ${chalk_1.default.bold('EXPORT ID'.padEnd(48))} ${chalk_1.default.bold('SINK'.padEnd(12))} ${chalk_1.default.bold('REQ'.padEnd(7))} ${chalk_1.default.bold('USED'.padEnd(10))} ${chalk_1.default.bold('READY'.padEnd(6))} ${chalk_1.default.bold('SOURCE PVC')}`);
            console.log('  ' + '─'.repeat(130));
            for (const s of items) {
                const id = s.exportId.padEnd(48).slice(0, 48);
                const sink = s.sink.padEnd(12);
                const size = (s.sizeGb === undefined ? '—' : `${s.sizeGb}Gi`).padEnd(7);
                const used = (s.actualBytes === undefined ? '—' : (0, format_bytes_1.formatBytes)(s.actualBytes)).padEnd(10);
                const ready = (s.ready ? chalk_1.default.green('OK') : chalk_1.default.yellow('..')).padEnd(6);
                const src = s.sourcePvcName ?? '—';
                console.log(`  ${id} ${sink} ${size} ${used} ${ready} ${src}`);
            }
            console.log('');
            console.log(chalk_1.default.dim(`  ${items.length} snapshot(s) total`));
            console.log('');
        }
        catch (error) {
            spinner.fail('Failed to list snapshots');
            const msg = error.response?.data?.message ?? error.message ?? String(error);
            console.log(chalk_1.default.red(`\n  Error: ${msg}\n`));
            this.exit(1);
        }
    }
}
AppSnapshotList.description = 'List volume snapshots. ' +
    'Use --app to filter to a single app, omit to list cluster-wide.';
AppSnapshotList.examples = [
    '<%= config.bin %> <%= command.id %>',
    '<%= config.bin %> <%= command.id %> --app my-app',
];
AppSnapshotList.flags = {
    cluster: core_1.Flags.string({
        char: 'c',
        description: 'Cluster name or ID (default: auto-detect)',
    }),
    app: core_1.Flags.string({
        char: 'a',
        description: 'Filter by application name or slug',
    }),
    output: core_1.Flags.string({
        char: 'o',
        description: 'Output format',
        options: ['table', 'json'],
        default: 'table',
    }),
};
exports.default = AppSnapshotList;
