"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 nest_app_1 = require("../../lib/nest-app");
const cli_observability_cluster_service_1 = require("../../services/cli-observability-cluster.service");
const cluster_storage_service_1 = require("../../../../src/modules/infrastructure/clusters/services/cluster-storage.service");
const cluster_storage_dto_1 = require("../../../../src/modules/infrastructure/clusters/dto/cluster-storage.dto");
const context_banner_1 = require("../../lib/context-banner");
class EnvStorage extends core_1.Command {
    async run() {
        (0, context_banner_1.printContextBanner)();
        const spinner = (0, ora_1.default)('Inspecting shared storage...').start();
        try {
            const app = await (0, nest_app_1.getNestApp)();
            const observabilityService = app.get(cli_observability_cluster_service_1.CliObservabilityClusterService);
            const storageService = app.get(cluster_storage_service_1.ClusterStorageService);
            const cluster = await observabilityService.getObservabilityCluster();
            if (!cluster) {
                spinner.fail('No observability cluster found');
                console.log(chalk_1.default.yellow('\n⚠️  Create a cluster first: ' + chalk_1.default.cyan('flui env create\n')));
                return;
            }
            const status = await storageService.getStatus(cluster.id);
            spinner.succeed('Storage status retrieved');
            this.render(status);
        }
        catch (error) {
            spinner.fail('Failed to retrieve storage status');
            console.log(chalk_1.default.red('\n❌ Error:\n'));
            console.log(`   ${error instanceof Error ? error.message : String(error)}\n`);
            this.exit(1);
        }
        finally {
            await (0, nest_app_1.closeNestApp)();
        }
    }
    render(s) {
        console.log(chalk_1.default.cyan('\n💾 Cluster Shared Storage\n'));
        console.log(`   ${chalk_1.default.bold('Status:')}  ${this.formatStatus(s.status)}`);
        console.log(`   ${chalk_1.default.bold('Enabled:')} ${s.enabled ? 'yes' : 'no'}`);
        if (s.message) {
            console.log(`   ${chalk_1.default.dim(s.message)}`);
        }
        if (s.volume) {
            console.log(chalk_1.default.cyan('\n📦 Backing Volume\n'));
            console.log(`   ${chalk_1.default.bold('Provider:')}    ${s.volume.provider}`);
            console.log(`   ${chalk_1.default.bold('Volume ID:')}   ${s.volume.volumeId}`);
            console.log(`   ${chalk_1.default.bold('Size:')}        ${s.volume.sizeGb} GB`);
            console.log(`   ${chalk_1.default.bold('Mount path:')}  ${s.volume.mountPath}`);
            console.log(`   ${chalk_1.default.bold('FS label:')}    ${s.volume.fsLabel}`);
        }
        if (s.nfs) {
            console.log(chalk_1.default.cyan('\n🌐 NFS Export\n'));
            console.log(`   ${chalk_1.default.bold('Export path:')} ${s.nfs.exportPath}`);
            console.log(`   ${chalk_1.default.bold('Server opts:')} ${chalk_1.default.dim(s.nfs.exportOptions)}`);
            console.log(`   ${chalk_1.default.bold('Client opts:')} ${chalk_1.default.dim(s.nfs.mountOptions)}`);
        }
        if (s.pvcs) {
            console.log(chalk_1.default.cyan('\n📂 PersistentVolumeClaims\n'));
            console.log(`   ${chalk_1.default.bold('Bound PVCs:')}    ${s.pvcs.bound}`);
            console.log(`   ${chalk_1.default.bold('Requested:')}     ${s.pvcs.requestedGb} GB`);
            const namespaces = Object.entries(s.pvcs.byNamespace);
            if (namespaces.length > 0) {
                console.log(`   ${chalk_1.default.bold('By namespace:')}`);
                const sortedNamespaces = [...namespaces].sort(([a], [b]) => a.localeCompare(b));
                for (const [ns, count] of sortedNamespaces) {
                    console.log(`     ${chalk_1.default.dim('•')} ${ns}: ${count}`);
                }
            }
        }
        console.log('');
    }
    formatStatus(status) {
        const map = {
            [cluster_storage_dto_1.ClusterStorageStatus.READY]: chalk_1.default.green,
            [cluster_storage_dto_1.ClusterStorageStatus.PROVISIONING]: chalk_1.default.yellow,
            [cluster_storage_dto_1.ClusterStorageStatus.DEGRADED]: chalk_1.default.yellow,
            [cluster_storage_dto_1.ClusterStorageStatus.ERROR]: chalk_1.default.red,
            [cluster_storage_dto_1.ClusterStorageStatus.DISABLED]: chalk_1.default.dim,
            [cluster_storage_dto_1.ClusterStorageStatus.UNKNOWN]: chalk_1.default.white,
        };
        return (map[status] ?? chalk_1.default.white)(status);
    }
}
EnvStorage.description = 'Show shared storage status (Volume + NFS export + PVC summary) for the current observability cluster';
EnvStorage.examples = ['<%= config.bin %> <%= command.id %>'];
exports.default = EnvStorage;
