"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_node_service_1 = require("../../lib/services/cli-node.service");
const resolve_cluster_1 = require("../../lib/resolve-cluster");
class NodeList extends core_1.Command {
    async run() {
        const { flags } = await this.parse(NodeList);
        const spinner = (0, ora_1.default)('Fetching nodes...').start();
        try {
            const { id: clusterId } = await (0, resolve_cluster_1.resolveCluster)(flags.cluster);
            const service = await cli_node_service_1.CliNodeService.create(clusterId);
            const nodes = await service.listNodes();
            spinner.stop();
            if (flags.output === 'json') {
                console.log(JSON.stringify(nodes, null, 2));
                return;
            }
            if (nodes.length === 0) {
                console.log(chalk_1.default.yellow('\n  No nodes found.\n'));
                return;
            }
            console.log(chalk_1.default.cyan('\n  Cluster Nodes\n'));
            console.log(chalk_1.default.dim(`  ${'ID'.padEnd(38)} ${'ROLE'.padEnd(10)} ${'STATUS'.padEnd(12)} ${'IP'.padEnd(18)} NAME`));
            console.log(chalk_1.default.dim('  ' + '─'.repeat(96)));
            for (const node of nodes) {
                const name = node.serverName.length > 30
                    ? node.serverName.slice(0, 29) + '…'
                    : node.serverName;
                const role = node.nodeType;
                const roleColored = role === 'master'
                    ? chalk_1.default.magenta(role.padEnd(10))
                    : chalk_1.default.dim(role.padEnd(10));
                const statusPadded = (node.status || 'unknown').padEnd(12);
                const statusColored = this.colorStatus(statusPadded);
                const ip = (node.ipAddress || '-').padEnd(18);
                console.log(`  ${node.id.padEnd(38)} ${roleColored} ${statusColored} ${ip} ${name}`);
            }
            const workers = nodes.filter((n) => n.nodeType === 'worker').length;
            console.log('');
            console.log(chalk_1.default.dim(`  ${nodes.length} node${nodes.length === 1 ? '' : 's'} total (1 master, ${workers} worker${workers === 1 ? '' : 's'})`));
            console.log('');
        }
        catch (error) {
            spinner.fail('Failed to fetch nodes');
            console.log(chalk_1.default.red(`\n  Error: ${error.message}\n`));
            this.exit(1);
        }
    }
    colorStatus(status) {
        const s = status.trim().toLowerCase();
        if (s === 'ready')
            return chalk_1.default.green(status);
        if (s === 'notready' || s === 'not_ready')
            return chalk_1.default.red(status);
        if (s === 'creating' || s === 'provisioning')
            return chalk_1.default.blue(status);
        return chalk_1.default.dim(status);
    }
}
NodeList.description = 'List all nodes in the cluster (master + workers)';
NodeList.examples = [
    '<%= config.bin %> <%= command.id %>',
    '<%= config.bin %> <%= command.id %> --output json',
];
NodeList.flags = {
    cluster: core_1.Flags.string({
        char: 'c',
        description: 'Cluster name or ID (default: auto-detect when only one cluster exists)',
    }),
    output: core_1.Flags.string({
        char: 'o',
        description: 'Output format',
        options: ['table', 'json'],
        default: 'table',
    }),
};
exports.default = NodeList;
