"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 context_banner_1 = require("../../lib/context-banner");
const cli_observability_cluster_service_1 = require("../../services/cli-observability-cluster.service");
const cli_node_repository_1 = require("../../lib/repositories/cli-node.repository");
const cluster_node_entity_1 = require("../../../../src/modules/infrastructure/clusters/entities/cluster-node.entity");
const cli_ssh_service_1 = require("../../services/cli-ssh.service");
class EnvUncordon extends core_1.Command {
    async run() {
        const { args } = await this.parse(EnvUncordon);
        (0, context_banner_1.printContextBanner)();
        const spinner = (0, ora_1.default)('Resolving node...').start();
        try {
            const app = await (0, nest_app_1.getNestApp)();
            const observabilityService = app.get(cli_observability_cluster_service_1.CliObservabilityClusterService);
            const nodeRepo = app.get(cli_node_repository_1.CliNodeRepository);
            const ssh = app.get(cli_ssh_service_1.CliSshService);
            const cluster = await observabilityService.getObservabilityCluster();
            if (!cluster) {
                spinner.fail('No observability cluster found');
                return;
            }
            const nodes = await nodeRepo.find({ where: { clusterId: cluster.id } });
            const target = args.target === 'master'
                ? nodes.find((n) => n.nodeType === cluster_node_entity_1.NodeType.MASTER)
                : nodes.find((n) => n.serverName === args.target);
            if (!target) {
                spinner.fail(`Node "${args.target}" not found`);
                this.exit(1);
            }
            if (!cluster.masterIpAddress) {
                spinner.fail('Cluster has no masterIpAddress stored');
                this.exit(1);
            }
            spinner.text = `Uncordoning ${target.serverName} via SSH to master...`;
            const out = await ssh.sshExec(cluster.masterIpAddress, `kubectl uncordon ${target.serverName}`);
            spinner.succeed(`Node ${target.serverName} is now schedulable`);
            if (out.trim()) {
                console.log(chalk_1.default.dim(`   ${out.trim()}`));
            }
            console.log('');
        }
        catch (error) {
            spinner.fail('Uncordon failed');
            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)();
        }
    }
}
EnvUncordon.description = 'Mark a cluster node schedulable again. Recovery helper for when a scale-node ' +
    'operation interrupted before reaching the uncordon step.';
EnvUncordon.examples = [
    '<%= config.bin %> <%= command.id %> master',
    '<%= config.bin %> <%= command.id %> worker-1',
];
EnvUncordon.args = {
    target: core_1.Args.string({
        description: 'Node target: "master" or a worker node serverName',
        required: true,
    }),
};
exports.default = EnvUncordon;
