"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 HetznerBootstrapSeeder_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HetznerBootstrapSeeder = void 0;
const common_1 = require("@nestjs/common");
const cloud_provider_enum_1 = require("../../enums/cloud-provider.enum");
const credentials_entity_1 = require("../../../management/entities/credentials.entity");
/**
 * Hetzner-specific bootstrap seeder logic.
 *
 * Reads `PROVIDER_HETZNER_API_KEY` from the pod env and resolves the master
 * server's numeric ID (via Hetzner API by-name lookup) when the CLI has only
 * the placeholder name available.
 */
let HetznerBootstrapSeeder = HetznerBootstrapSeeder_1 = class HetznerBootstrapSeeder {
    constructor() {
        this.provider = cloud_provider_enum_1.CloudProvider.HETZNER;
        this.logger = new common_1.Logger(HetznerBootstrapSeeder_1.name);
    }
    buildCredentials(env) {
        const token = env.PROVIDER_HETZNER_API_KEY;
        if (!token)
            return null;
        return {
            credentialType: credentials_entity_1.CredentialType.API_KEY,
            token,
            label: 'Hetzner API Token (bootstrap)',
        };
    }
    async resolveProviderResourceId(args) {
        const { instanceId, instanceName, env } = args;
        // Hetzner native IDs are positive integers — short-circuit when CLI already
        // gave us one.
        if (instanceId && /^\d+$/.test(instanceId))
            return instanceId;
        const token = env.PROVIDER_HETZNER_API_KEY;
        if (!token || !instanceName)
            return instanceId;
        try {
            const res = await fetch(`https://api.hetzner.cloud/v1/servers?name=${encodeURIComponent(instanceName)}`, { headers: { Authorization: `Bearer ${token}` } });
            if (!res.ok) {
                this.logger.warn(`Hetzner lookup for ${instanceName} returned ${res.status}; keeping placeholder`);
                return instanceId;
            }
            const data = (await res.json());
            const id = data.servers?.[0]?.id;
            if (id) {
                this.logger.log(`Resolved Hetzner numeric ID for ${instanceName}: ${id}`);
                return String(id);
            }
            this.logger.warn(`Hetzner lookup for ${instanceName} returned no servers; keeping placeholder`);
        }
        catch (err) {
            this.logger.warn(`Hetzner lookup failed for ${instanceName}: ${err.message}; keeping placeholder`);
        }
        return instanceId;
    }
};
exports.HetznerBootstrapSeeder = HetznerBootstrapSeeder;
exports.HetznerBootstrapSeeder = HetznerBootstrapSeeder = HetznerBootstrapSeeder_1 = __decorate([
    (0, common_1.Injectable)()
], HetznerBootstrapSeeder);
