"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 __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var HetznerSSHProviderService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HetznerSSHProviderService = void 0;
const common_1 = require("@nestjs/common");
const config_1 = require("@nestjs/config");
const cloud_provider_enum_1 = require("../../providers/enums/cloud-provider.enum");
const generated_1 = require("../../../modules/providers/implementations/hetzner/generated");
let HetznerSSHProviderService = HetznerSSHProviderService_1 = class HetznerSSHProviderService {
    constructor(configService) {
        this.configService = configService;
        this.logger = new common_1.Logger(HetznerSSHProviderService_1.name);
        this.basePath = this.configService.get('HETZNER_API_BASE_PATH', 'https://api.hetzner.cloud/v1');
    }
    async listSSHKeys() {
        try {
            // Get API token directly from environment or config
            // This avoids circular dependency with AccessService
            const apiToken = this.configService.get('HETZNER_API_TOKEN');
            if (!apiToken) {
                this.logger.warn('No Hetzner API token configured');
                return [];
            }
            const configuration = new generated_1.Configuration({
                accessToken: apiToken,
                basePath: this.basePath,
            });
            const sshKeysApi = new generated_1.SSHKeysApi(configuration);
            const response = await sshKeysApi.listSshKeys();
            return response.data.ssh_keys
                .filter((sshKey) => this.isFluiManaged(sshKey.labels))
                .map((sshKey) => this.mapHetznerSSHKeyToDto(sshKey));
        }
        catch (error) {
            this.logger.error('Failed to list SSH keys from Hetzner API', error);
            return [];
        }
    }
    async testConnection() {
        try {
            await this.listSSHKeys();
            return { success: true };
        }
        catch (error) {
            return { success: false, error: error.message };
        }
    }
    mapHetznerSSHKeyToDto(sshKey) {
        return {
            id: sshKey.id.toString(),
            name: sshKey.name,
            publicKey: sshKey.public_key,
            fingerprint: sshKey.fingerprint,
            type: this.extractKeyType(sshKey.public_key),
            createdAt: new Date(sshKey.created),
            updatedAt: new Date(sshKey.created),
            source: cloud_provider_enum_1.CloudProvider.HETZNER,
            syncedFromProvider: true,
            providerKeyId: sshKey.id.toString(),
            isActive: sshKey.is_active,
            autoGenerated: false,
        };
    }
    isFluiManaged(labels) {
        if (!labels)
            return false;
        return (labels['managed-by'] === 'flui-cloud' &&
            labels['flui-resource-type'] === 'ssh-key');
    }
    extractKeyType(publicKey) {
        if (publicKey.startsWith('ssh-rsa'))
            return 'rsa';
        if (publicKey.startsWith('ssh-ed25519'))
            return 'ed25519';
        if (publicKey.startsWith('ssh-dss'))
            return 'dsa';
        if (publicKey.startsWith('ecdsa-sha2'))
            return 'ecdsa';
        return 'unknown';
    }
};
exports.HetznerSSHProviderService = HetznerSSHProviderService;
exports.HetznerSSHProviderService = HetznerSSHProviderService = HetznerSSHProviderService_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [config_1.ConfigService])
], HetznerSSHProviderService);
