"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var LabelService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LabelService = void 0;
const common_1 = require("@nestjs/common");
let LabelService = LabelService_1 = class LabelService {
    constructor() {
        this.logger = new common_1.Logger(LabelService_1.name);
        this.FLUI_VERSION = '1.0.0';
    }
    /**
     * Generate standard Flui labels for a server/node
     * @returns Array of label objects
     */
    generateServerLabels(config) {
        const labels = [
            { key: 'managed-by', value: 'flui-cloud' },
            // { key: 'flui-version', value: this.FLUI_VERSION }, // Removed - contains dots
            { key: 'flui-resource-type', value: config.resourceType },
            // { key: 'flui-created-at', value: new Date().toISOString() }, // Removed - contains colons and dots
        ];
        if (config.resourceType === 'cluster-node') {
            if (!config.clusterId || !config.clusterName) {
                throw new Error('clusterId and clusterName required for cluster nodes');
            }
            labels.push({ key: 'flui-cluster-id', value: config.clusterId }, { key: 'flui-cluster-name', value: config.clusterName });
            if (config.nodeId) {
                labels.push({ key: 'flui-node-id', value: config.nodeId });
            }
            if (config.nodeType) {
                labels.push({ key: 'flui-node-type', value: config.nodeType });
            }
        }
        if (config.environment) {
            labels.push({ key: 'flui-environment', value: config.environment });
        }
        return labels;
    }
    /**
     * Convert label array to Record for provider compatibility
     */
    toRecord(labels) {
        return labels.reduce((acc, label) => {
            acc[label.key] = label.value;
            return acc;
        }, {});
    }
    /**
     * Convert Record to label array
     */
    fromRecord(record) {
        if (!record)
            return [];
        return Object.entries(record).map(([key, value]) => ({ key, value }));
    }
    /**
     * Validate if a server is managed by Flui
     */
    isFluiManagedServer(labels) {
        if (!labels || labels.length === 0)
            return false;
        return labels.some((l) => l.key === 'managed-by' && l.value === 'flui-cloud');
    }
    /**
     * Get label value by key
     */
    getLabelValue(labels, key) {
        return labels.find((l) => l.key === key)?.value;
    }
    /**
     * Extract cluster info from labels
     */
    getClusterInfoFromLabels(labels) {
        return {
            clusterId: this.getLabelValue(labels, 'flui-cluster-id'),
            clusterName: this.getLabelValue(labels, 'flui-cluster-name'),
            nodeId: this.getLabelValue(labels, 'flui-node-id'),
            nodeType: this.getLabelValue(labels, 'flui-node-type'),
        };
    }
    /**
     * Generate standard Flui labels for an SSH key
     * @returns Array of label objects
     */
    generateSSHKeyLabels(config) {
        const labels = [
            { key: 'managed-by', value: 'flui-cloud' },
            { key: 'flui-resource-type', value: 'ssh-key' },
            { key: 'flui-ssh-key-id', value: config.sshKeyId },
            { key: 'flui-ssh-key-name', value: config.sshKeyName },
        ];
        if (config.environment) {
            labels.push({ key: 'flui-environment', value: config.environment });
        }
        return labels;
    }
    /**
     * Validate if an SSH key is managed by Flui
     */
    isFluiManagedSSHKey(labels) {
        if (!labels)
            return false;
        const labelArray = Array.isArray(labels) ? labels : this.fromRecord(labels);
        return (labelArray.some((l) => l.key === 'managed-by' && l.value === 'flui-cloud') &&
            labelArray.some((l) => l.key === 'flui-resource-type' && l.value === 'ssh-key'));
    }
    /**
     * Extract SSH key info from labels
     */
    getSSHKeyInfoFromLabels(labels) {
        const labelArray = Array.isArray(labels) ? labels : this.fromRecord(labels);
        return {
            sshKeyId: this.getLabelValue(labelArray, 'flui-ssh-key-id'),
            sshKeyName: this.getLabelValue(labelArray, 'flui-ssh-key-name'),
            environment: this.getLabelValue(labelArray, 'flui-environment'),
        };
    }
};
exports.LabelService = LabelService;
exports.LabelService = LabelService = LabelService_1 = __decorate([
    (0, common_1.Injectable)()
], LabelService);
